2

I was wondering which is the best way (the one suggested by Apple guidelines or by common sense) to store big files for an iOS application..

With big I mean for example the save of a game (which can be, let's say 500kb) so that thinking about plists is unfeasible.

Should I just save it using normal Cocoa file management API? What about standard stdio (fopen, fwrite, whatever)?

Jack
  • 131,802
  • 30
  • 241
  • 343
  • Looks like there is already an API for writing whatever you want to a file. http://stackoverflow.com/questions/1526416/iphone-writing-binary-data I'm not sure that there is a limitation; at least i haven't found anything. – Suroot Jul 08 '11 at 21:38

2 Answers2

3

Your top priority should be overall user experience, including:

  1. Keep your app responsive: save only what changed. If you don't have to save 500k all at once, but only a smaller portion that changed, you may want to consider sqlite or multiple smaller files.
  2. Use the device's resources in moderation: if your app uses up a disproportionate amount of available storage space, users are more likely to delete it.
  3. Use the API that fits best with your approach to 1 & 2 above. There's no point going to extra lengths with a particular API if it doesn't improve user experience.

In my experience, big file operations tend to be relatively slow, but the cpu is relatively fast, so if you have a huge chunk of text to save, using zlib to compress it for saving works well (text compression rates are high, and zlib is pretty fast).

odrm
  • 5,149
  • 1
  • 18
  • 13
2

There are plenty of options, and of course the best one depends on your requirements. You can certainly use the C standard I/O stuff if you want. If you've got all your game data in one big buffer, NSData's read from/write to file methods are easy to use. If you've got a collection of objects that implement NSCoding, you can use NSKeyedArchiver/Unarchiver.

Here's one way to use NSFileManager to locate your app's Documents directory:

NSString *docsDir = [NSHomeDirectory() stringByAppendingPathComponent: @"Documents"];
Caleb
  • 124,013
  • 19
  • 183
  • 272
  • I can easily manage everything with standard I/O, my main worry was where to store the file, maybe there are restrictions or whatever? I'm really unaware in how the application is actually managed by iOS (its working directory or "data" directory or whatever) – Jack Jul 08 '11 at 21:39
  • Use [NSFileManager](http://tinyurl.com/4eflxtl) to locate your app's Documents directory and write your data there. Each app has its own little space in the file system, and you can't read or write outside that space. The Documents directory will be backed up when the user syncs their device, which is also useful. Example added to answer above. – Caleb Jul 08 '11 at 21:43