If an app starts saving virtually unlimited images (on demand) to the /Library/Caches directory for caching purpose, what will happen when the storage is full? Will saving to the caches folder result in error? OR If the app is open, can the cache of the open app be purged by iOS automatically? If not, is it the responsibility of the developer to initiate this purging process for the open app? OR Is it guaranteed that an attempt to save an image to /Library/Caches will always be successful regardless of whether the purging takes place or not? (Cached images are cleaned by the app on exit).
Yes, it may result in error. No, it will not be purged by iOS automatically while an app is running. Quote from the docs:
Caches
On iOS 5.0 and later, the system may delete the Caches directory on rare occasions when the system is very low on disk space. This will never occur while an app is running
So it's your responsibility to cleanup the caches directory while running. However you would probably meet this error in a very rare cases since iOS will try to remove caches of other apps that are not running.
Related
I have an iOS application that fetches network resources by using URLRequest with default cachePolicy. I would like to know if:
When the application is deleted from my device, is all cached responses are removed as well? Or do they stay somewhere in the device? (until they are expired)
If yes, if I install the app again in that device, when I launch the app, it will get the cached response stored in iOS device instead of fetching original resources from back-end (if the responses are still valid, of course)?
Below is the behaviour of default Cache Policy explained by Apple:
Thanks.
The caches are stored in a file in your app’s container directory (specifically, Caches/BUNDLE_ID/Cache.db in iOS, or Library/Caches/BUNDLE_ID/Cache.db in macOS, IIRC). When the app goes away, so does the cache, and as far as I’m aware, they are never stored in iCloud backups or anything, so there should be no possibility of them resurfacing.
But be aware that other things can cache responses (e.g. proxy servers on the local network), so if your goal is to completely eliminate any possibility of getting a stale response, you should explicitly disable caching for the request.
If your goal is to have a prewarmed cache, you could distribute a cache file in your app bundle and make a copy of it on first launch before enabling the disk cache, but you are probably better off downloading a ZIP archive and managing files on disk yourself if you are trying to do any sort of offline mode, rather than trying to bend NSURLCache to your will.
I have some application state data that I am storing in the Library Caches folder, and have been noticing when I delete the app from my device, then build and run the app again, the same file exists.
Is this just a symptom of debug development or does this behaviour actually occur in production? Where else am I to store such files if I really want them to only exist per installation?
I have a very strange issue that has been occurring to a few users of my app. One of the users actually described the issue to me.
My app downloads magazine data to the device to allow for offline reading. This means that users can sometimes have around 10 gigs+ downloaded to the device.
The problem this user experienced (and a few others have too) is that randomly all sandbox data gets deleted from the app (included core data files). The user told me that he was downloading something in a different app and got the "Storage Almost Full - You can manage your storage in Settings" popup message.
He went to the settings app and went to General > Usage. He then saw the app was using around 13 gigs of data. While he was in that list he said every time he went out of the Usage tab, then back in the app's data was getting smaller and smaller. Until eventually the app said it was using 0mb.
When he logged back into the app all his data was deleted and core data was also removed which lead the app to think he was a completely new user with no data downloaded.
I then ran some tests on my own where I made sure my device only had 100mb of space available. I then started downloading in the app. The warning message popped up alerting me I was running out of space and I ignored it and continued downloading. I have now downloaded about 1.5 gigs of data and am still downloading and the device seems like it is freeing up space somewhere else to make room for my downloads.
I've searched far and wide and have found no one else ever experiencing a problem like this. So my questions are:
Has anyone else every experienced this issue before?
Does Apple have a policy that will remove app data from the app that uses the most space if the device is running out of space?
Is there an algorythm that decides which app will get cleared when space is running out?
Is there a way to tell the OS not to remove data when running out of space?
Any help would be appreciated!
Assuming you're storing the data in the caches subdirectory:
On iOS 5.0 and later, the system may delete the Caches directory on
rare occasions when the system is very low on disk space. This will
never occur while an app is running. However, you should be aware that
iTunes restore is not necessarily the only condition under which the
Caches directory can be erased.
Documentation here
In response to Apple's changes in iOS data storage guidelines I recently reconfigured an iPad app to store its documents (50MB+) in the library/caches folder.
During testing in the iOS simulator I didn't see any problems with this cache when I simulated an update (following Brad Larsson's suggestion). I also tried deleting all of the contents of the library/caches folder to make sure that my app could recover.
However, when I released the app upgrade I found that my users complained of problems that were traced back to the iPad partially deleting this cache. It seemed like sub-folder structure was at least partially left intact, but sub-folder contents were deleted.
I've redesigned the app to deal with this situation during an upgrade, but I'm worried about what will happen when the iPad decides to clear the library/caches.
Does anyone have any experience or insight into the time when the iPad would try and delete items in this cache?
Thanks
There is no 100% clear answer to this question, because Apples iOS Data Storage Guidelines are very vague … They don't explain in which cases iOS 5 will delete data inside the cache dir …
In most cases iOS starts to kill files when it's getting low on disc space, but sometimes my app lost data for no good reason. So I had do implement some kind of recovery modus to redownload/-generate files the app needs.
This article is interesting: http://iphoneincubator.com/blog/data-management/local-file-storage-in-ios-5
The documentation states the following:
On iOS 5.0 and later, the system may delete the Caches directory on
rare occasions when the system is very low on disk space. This will
never occur while an app is running. However, you should be aware that
iTunes restore is not necessarily the only condition under which the
Caches directory can be erased.
The part about it never occuring while the app is running is crucial for our app, and is a really good insurance that we won't get unexpected behaviour.
Is it possible for the Caches directory to be purged while my app is running, and if so, will my app be notified in any way that this has occured?
Thanks!
I don't know if it is possible for the cleaning to occur while it is running (though my impression is no). However if you want to detect when the cleaning has occurred you can set a test file into the caches directory and check for it's presence.
File Gone = Cache Purged
No, according to the docs:
On iOS 5.0 and later, the system may delete the Caches directory on rare occasions when the system is very low on disk space. This will never occur while an app is running. However, you should be aware that iTunes restore is not necessarily the only condition under which the Caches directory can be erased.