开发者

Save big files on an iOS application

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 开发者_StackOverflow社区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)?


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).


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"];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜