iphone - 将 NSCache 内容保存到磁盘

标签 iphone cocoa-touch cocoa nscache

我正在编写一个应用程序,需要保留一堆对象的内存缓存,但这不会失控,因此我计划使用 NSCache 来存储所有内容。看起来它会负责清理等工作,这对我来说太棒了。

我还想在启动之间保留缓存,因此我需要将缓存数据写入磁盘。有没有一种简单的方法可以将 NSCache 内容保存到 plist 或其他内容中?除了 NSCache 之外,是否有更好的方法来完成此任务?

此应用程序将在 iPhone 上运行,因此我只需要 iOS 4+ 中提供的类,而不仅仅是 OS X 中提供的类。

谢谢!

最佳答案

I'm writing an app that needs to keep an in-memory cache of a bunch of objects, but that doesn't get out of hand so I'm planning on using NSCache to store it all. Looks like it will take care of purging and such for me, which is fantastic. I'd also like to persist the cache between launches, so I need to write the cache data to disk. Is there an easy way to save the NSCache contents to a plist or something? Are there perhaps better ways to accomplish this using something other than NSCache?

您几乎刚刚准确地描述了 CoreData 的作用;具有清除和修剪功能的对象图的持久性。

NSCache 并不是为了协助持久性而设计的。

鉴于您建议坚持使用 plist 格式,使用 Core Data 来代替并没有那么大的概念差异。

关于iphone - 将 NSCache 内容保存到磁盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4542707/

相关文章:

macos - 从 NSTask 获取任务进度通知

objective-c - 声明类

iOS - 完整上传带有 EXIF 的图像

ios - UICollectionReusableView 中的标签仅在偶数索引中显示

iphone - 使用 CATransition 重新创建 iOS 应用打开过渡

ios - "On"UISwitch 的状态

iphone - 适用于 iOS 的 Xcode 项目自动化

ios - 如何在我的手机上安装我自制的 iPhone 应用程序

iphone - "Creating precompiled collator because collator is out of date"

macos - 如何防止我的搜索字段成为弹出窗口中的初始第一响应者?