views:

441

answers:

2

I have several large files that I want to load into the applications document folder and then call up from there on demand.

The suggestion I've seen is to put them in the resources folder and then when the application loads copy them into the documents folder. I assume that if I do this, these large files will all be loaded into memory when the application starts up and then stay there...

Is there some way of having these files go directly into the documents folder when the application is installed, and get loaded into memory only when they are called for?

Thanks...

A: 

There's no way to do this automatically at the installation.

You can use NSFileManager like that to copy your files.

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *finalPath = [documentsDirectory stringByAppendingPathComponent:@"documentName"];
[[NSFileManager defaultManager] copyItemAtPath:yourDocumentPath toPath:finalPath error:nil];
gcamp
Thanks. Am I right in assuming that those large files in the resources directory will get loaded into memory and stay there? Is there a way of unloading them after copying them to the documents directory?
ilmar
Modifying the installed app bundle (i. e. erasing the files) is heavily recommended against. Not exactly sure what would be the repercussions, but Apple says - don't.What keeps you from using these files straight from the bundle? It's not like the user will be able to tell the difference.
Seva Alekseyev
I'm concerned about the hit on memory from keeping these large files there.. also, the app will take longer to load.. normally they would need to access only one of the large files at a time..
ilmar
There will be no hit on memory. There won't be loaded on memory unless you do so. It's not because it's on the document folder that it's on memory. (same for the load time).Also, NSFileManger is like 'cd' it doesn't load on memory the files, it just copy them on disk.And if you change you app bundle, your app just won't load (code signing).
gcamp
The bundle is just another folder.
Seva Alekseyev
ok.. thanks.. that was what I had misunderstood.. I had assumed the resources would all be loaded at once.
ilmar
A: 

Depends on where do the files originally reside. If in your bundle, then use NSFileManager, like gcamp said. If you download them from the 'Net, or generate them on the fly, use NSOutputStream - that's the API for writing binary or text data from memory into files. Or use POSIX open()/write(), old school.

Seva Alekseyev
What I would like to accomplish is to have these files loaded onto the iphone when the app is installed, but loaded into memory only when the particular file is being called for. Any way to do that?
ilmar
Um, when the app is installed, all resource files in your bundle are installed on the phone, too. When the app is run by the user, the resources are not loaded into memory automatically - until your code loads them somehow (using loadNibFile, imageNamed or some other method).
Seva Alekseyev
ok.. thanks.. that was what I had misunderstood.. I had assumed the resources would all be loaded at once..
ilmar