iOS Documents Directory size limit
I don't know much about this, I had seen a comment in discussions.apple.com like:
There is a limit of 2 GB.
https://discussions.apple.com/message/7599983?messageID=7599983#7599983?messageID=7599983
Files in the document directory stay there permanently until the app is removed.
Maximum size of document directory in Iphone?
The links bellow may be of some help:
http://www.iphonedevsdk.com/forum/iphone-sdk-development/4083-limit-restrictions-filesize-filecount-app-folder.html
https://discussions.apple.com/message/7599983?messageID=7599983#7599983?messageID=7599983
What is the file size limit in temporary directory in iOS?
It worked this way. Instead of downloading data using NSData(contentsOf:)
, I used URLSession.shared.downloadTask(with:)
. This stores the downloaded data by itself so no need for us to write it. It just has to be moved to desired temporary location using FileManager.shared.moveItem(at:to:)
. Looks like the problem was with NSData.write(to:options:)
function. It has some size limit on it.
URLSession.shared.downloadTask(with: url) { (location, response, error) in
if let location = location {
let tmpDirectory = NSTemporaryDirectory()
let tmpFile = "file://".appending(tmpDirectory).appending(url.lastPathComponent)
let tmpUrl = URL(string: tmpFile)!
try! FileManager.default.moveItem(at: location, to: tmpUrl)
if let attachment = try? UNNotificationAttachment(identifier: "notification-img", url: tmpUrl) {
bestAttemptContent.attachments = [attachment]
}
}
contentHandler(bestAttemptContent)
}.resume()
Does iOS purge disk usage in the document directory?
You can find the docs that explain about the various options for storing data here:
https://developer.apple.com/library/archive/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/FileSystemOverview/FileSystemOverview.html
Specifically, check out Table 1-1 Commonly used directories of an iOS app
which should answer all your questions.
There is no limit on the amount of data you can store there, other than the on-device storage available.
However, the documents directory is only to be used for "user-generated content". It will be backed up to iCloud, so it's just for files the user would expect to be backed up. If the review team think you are using it to store support files that should be in the
temp
directory (which the system does purge when low on space), your app will fail review.The
The iCloud File Storage Container
section goes into specifics about the kind of thing that is considered user data:"Documents that the user creates and sees in an app's user interface—for example the document browsers in Pages, Numbers, and Keynote should be stored in the Documents directory. Another example of files that might go in the Documents directory are saved games, again because they are something that an app could potentially provide some sort of method for selecting.
Anything that the app does not want the user to see or modify directly should be placed outside of the Documents directory. Apps can create any subdirectories inside the container directory, so they can arrange private files as desired."
The contents of the Documents directory are not purged by the system when low on space.
What happens when you overflow iOS app Documents directory?
From my experience of downloading huge amounts of data and pre-caching them in the app's bundle (not in the Documents directory though), there is no filesystem size limit that iOS imposes for the single app.
The only limit is the free disk space of iOS device. Once you reach that limit, iOS triggers a cleanup on all the apps that are installed on the device, cleaning Library/Caches
and tmp
folders inside the bundle of all installed apps.
I did not test this yet, but there seems to be an NSBundleResourceRequestLowDiskSpace notification that is posted by the system when free disk space gets low.
What is the limit of number of resource files on an iOS app?
There is no limit on the files, as the functions to look up resources simply ask the file system API to look up the item in the app's directory. And that file system is using the HFS+ format, which has no practical limit on the files that it can manage inside a single directory. HFS Standard (the older format before HFS+) had a limit of 64k files per dir, IIRC, but the limit for HFS+ is 2^31, which is practically unlimited.
To add to your related question: One drawback of having 1000s of files in a dir is that it might take a while to create them all, meaning that the installation of your app may take longer than if you had the data all contained in a single file. The most effective way might be to use a container file, e.g. a zip (without compression), for which exist several iOS/ObjC libs.
Related Topics
How to Make a Swiftui Gesture That Keeps Running Code While the View Is Pressed
Missing Compliance Status in Testflight
Cocoapods Setup Stuck on Pod Setup Command on Terminal
Saving PDF Files with Swift in iOS and Display Them
iOS How to Asynchronously Download and Cache Images and Videos for Use in My App
Obscure a Uitextfield Password
"The Data Couldn't Be Read Because It Is Missing" Error When Decoding JSON in Swift
Warp \ Bend Effect on a Uiview
Swift Read Userinfo of Remote Notification
Avaudiosession Setcategory Swift 4.2 iOS 12 - Play Sound on Silent
How to Delete Object from Core Data in Swift 3
How to Use Git Properly with Xcode
Macos on Vmware Doesn't Recognize iOS Device
Find Where Object Is Retained with Arc
How to Get All Enum Values as an Array
How to Scroll to the Exact End of the Uitableview