在 iOS 应用程序上保存大文件

发布于 2024-11-19 10:24:09 字数 210 浏览 5 评论 0原文

我想知道哪种方法是为 iOS 应用程序存储大文件的最佳方法(Apple 指南或常识建议的方法)。

大我指的是例如游戏的保存(可以是 500kb)因此考虑 plist 是不可行的。

我应该使用普通的 Cocoa 文件管理 API 来保存它吗?标准 stdiofopenfwrite 等)怎么样?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

幽梦紫曦~ 2024-11-26 10:24:09

您的首要任务应该是总体用户体验,包括:

  1. 保持应用程序响应:仅保存更改的内容。如果您不必一次保存 500k 全部文件,而只需保存更改的一小部分,您可能需要考虑 sqlite 或多个较小的文件。
  2. 适度使用设备资源:如果您的应用程序使用了过多的可用存储空间,用户更有可能删除它。
  3. 使用最适合您的 1 和 2 方法的 API 2 上面。如果不能改善用户体验,那么就没有必要对特定的 API 进行额外的设计。

根据我的经验,大文件操作往往相对慢,但CPU相对快,因此如果您有大量文本要保存,请使用zlib来压缩它的保存效果很好(文本压缩率很高,而且 zlib 相当快)。

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

清君侧 2024-11-26 10:24:09

有很多选择,当然最好的选择取决于您的要求。如果你愿意的话,你当然可以使用 C 标准 I/O 的东西。如果您将所有游戏数据存储在一个大缓冲区中,则 NSData 的读取/写入文件方法很容易使用。如果您有实现 NSCoding 的对象集合,则可以使用 NSKeyedArchiver/Unarchiver。

以下是使用 NSFileManager 查找应用程序的文档目录的一种方法:

NSString *docsDir = [NSHomeDirectory() stringByAppendingPathComponent: @"Documents"];

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"];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文