Does the iPhone / iPod touch cache data also on disk, or only in memory? - iphone

I am not sure, but I think I was reading a while ago that iPhone / iPod Touch don't cache anything to disk. They cache only in memory. So cache is not persistet when app quits. After relaunch of app it will re-fetch the data from the net. Is that true?

iPhone OS disables on-disk caching from NSURLCache on this platform. If you need on-disk caching support anyway, you can use http://github.com/rs/SDURLCache which gives back on-disk cache support on iPhone OS

You can explore this yourself using the iPhone Simulator, opening a terminal or using the Finder, and going to `~Library/Application Support/iPhone Simulator/User/Applications/ then opening whatever directory your application is in.
You'll see a Documents directory, a Library and a tmp directory.
I've never looked to see if UIWebView is caching anything, but I know that Core Data will.
If you just suck down data yourself using CFNetwork, I believe it's up to you to cache the data yourself by saving it to tmp or Documents if you want.
tmp will not get backed up by the iTunes Sync process, but Documents will, so don't use Documents for temporary cache. Otherwise you'll just waste space and slow down sync backups.
The Documents directory will persist through anything, including app upgrades, except outright deleting the application using the Springboard or doing a total device storage wipe.
Actually the Library directory has directories. called Cache, WebKit/LocalStorage, and Preferences. So that would suggest something or the other.
Don't write to the application bundle directory or bad things could happen.

iPhone apps can write data to disk.

Related

Can the contents of the NSCachesDirectory be deleted by the system while the app is open?

As title.
Is it possible that files that I store in the NSCachesDirectory can be deleted by the system (i.e. in low storage situations), while my is running in the foreground?
File System programming guide
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.
So the answer is - No, it is not possible for the NSCachesDirectory to be deleted while the app is running in the foreground.
However their wording is slightly ambiguous with regard to the app running in the background.

how many images can I cache in my iPhone app?

I'm wondering how many images I can store in the iPhone documents directory so that I don't get any memory issues. I'm torn between caching images or loading them from a webserver every time the user wants to access them. There most likely won't be more than 10-20 images cached at any given time...
Any thoughts?
There's a 2GB cap on the largest size of a single file in the /Documents folder. Other than that, you can store as many images as you want. iOS itself didn't put a limitation on how much space you can use on a device. So your space limit is the size of Flash on the device (i.e. 16GB, 32GB etc).
You might consider use the /Library/Caches instead because iCloud will attempt to sync the Documents folder (if entitlements are configured). I heard App being rejected by review for excessive use of the Documents folder. To get the path, call NSSearchPathForDirectoriesInDomains with NSCachesDirectory and NSUserDomainMask.
Though a user can determine the size of space you are occupying in System Settings.
You can probably cache as many files as available memory allows, but you absolutely should be safe having 10 - 20 images cached at any given time.
B.T.W., instead of using the documents directory, why not consider using the cache directory as He (+1 to him!) suggests?
Here is a related question that shows how to get the size of the directory.

What happens to the content in my app after i upgrade/update the version in iPad/iPhone

I am storing a lot of images, books, audios etc of my app in
Documents Directory/Library/caches folder.
What happens to all the content if the user updates the version of the app from the app store or from iTunes. Will all of them get erased or will they be untouched during the app updating process?
They will be untouched during the upgrade process. I would like to note however that the Caches folder is meant to be used for data that is just cached rather than data you expect to always be available. The OS reserves the right to get rid of anything in the caches folder. If you're asking this question then I assume you want the data to be persistent so you should be aware that since iOS 5, Apple specifically state that data in the caches directory can be deleted at any time (apart from whilst your app is running).
They are supposed to remain untouched. There are a lot of existing examples for this behavior - try checking any photo sharing application or games that download levels in-game.

how can i do Limit the file app size in ipad?

I want to write a app to download and manage files from the web. If I just put the file in the Document. Is there any limit of file size in the Directory?
can any one help me..
thanks in advance.
Just a warning with the introduction of iOS5 saving large files or files that can be regenerated into the documents directory is frowned upon (actually you were never meant to do it but its only being enforced now). This is because the files in the documents directory get backed up to iCloud and you obviously do not want to fill that up. Apple have started rejecting apps just for this reason.
You should now place the downloaded files into the temp or cache directories. But also be aware that the OS will clear these directories when running low on memory so you need to have logic in you app to check if the files exist and re-download if they do not.
No real limit except the device and other user data. Still best to be frugal.

Is Library/Caches cleared automatically by the iPhoneOS?

I have an app that will need to cache some images.
I have read some documentation about caching, and the logical thing to do is to cache my images within the Library/Caches directory within my app's sandbox.
I understand that the reason for storing caches images here are:
Library/Caches isn't backed up by iTunes
Library/Caches is cleared periodically by the OS
That second point is what I'm questioning...
Is this true?
Does the OS clear the caches directory automatically?
Will I need any of my own logic to detect if the cache is too big and to clear the oldest items?
I found this in the documentation:
Use this directory to write any application-specific support files that you want to persist between launches of the application. Your application is generally responsible for adding and removing these files. However, iTunes removes these files during a full restore of the device so you should be able to recreate them as needed. To access this directory, use the interfaces described in “Getting Paths to Application Directories” to get the path to the directory.
In iPhone OS 2.2 and later, the contents of this directory are not backed up by iTunes.
http://developer.apple.com/iphone/library/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/FilesandNetworking/FilesandNetworking.html