ios iOS上的Objective C数据缓存

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5733725/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 19:38:48  来源:igfitidea点击:

Objective C Data Caching on iOS

objective-ccocoa-touchios

提问by lavoy

I am pulling data from an API and then building out my data objects with it. I want to cache this data for the future. I have been storing the string from the api in NSUserDefaults and then re-parsing it each time the app loads. This works, but it really seems like the wrong way to go about it.

我从 API 中提取数据,然后用它构建我的数据对象。我想为将来缓存这些数据。我一直在 NSUserDefaults 中存储来自 api 的字符串,然后在每次应用加载时重新解析它。这有效,但它似乎确实是错误的方法。

How can I do this?

我怎样才能做到这一点?

采纳答案by lavoy

There are many different solutions to this problem and there is no "right" way to do it. A few popular options are:

这个问题有很多不同的解决方案,并且没有“正确”的方法来解决这个问题。一些流行的选项是:

  • Core Data- Apple's framework for persistence. Very performant, but more difficult.
  • SQLite- Fast and flexible, but bare bones.
  • Plists- Basically writing a file to disk, you have to read and write manually.
  • NSUserDefaults- The lightest weight "key-value" option.
  • Core Data- Apple 的持久性框架。性能非常好,但更难。
  • SQLite- 快速而灵活,但只是骨架。
  • Plists- 基本上是将文件写入磁盘,您必须手动读取和写入。
  • NSUserDefaults- 最轻量级的“键值”选项。

I would encourage you to read up on all four and see which one works best for you.

我鼓励你阅读所有四个,看看哪一个最适合你。

回答by Eric V

Have you noticed the NSCache?

你注意到NSCache 了吗?

An NSCacheobject is a mutable collection that stores key-value pairs, similar to an NSDictionaryobject. The NSCacheclass provides a programmatic interface to adding and removing objects and setting eviction policies based on the total cost and number of objects in the cache...

一个NSCache对象是一个可变集合,其存储键值对,类似于NSDictionary对象。本NSCache类提供的编程接口添加和删除对象和基于缓存的总成本和对象的数量设置逐出策略...

回答by Wolfgang Schreurs

Personally I'm quite fond of the EGOCache classes, I use them quite a lot in my projects:

我个人非常喜欢 EGOCache 类,我在我的项目中经常使用它们:

https://github.com/enormego/EGOCache

https://github.com/enormego/EGOCache

The classes are easy to use, I used to have my own classes with a similar design, but these are just more well-rounded, so I decided to stick with them (don't wanna reinvent the wheel).

这些类很容易使用,我曾经有过类似设计的自己的类,但这些类更全面,所以我决定坚持使用它们(不想重新发明轮子)。

回答by Chris Wagner

I vote Core Data

我投票给核心数据

回答by Jiva DeVoe

I'd store the computed/parsed data in either a Core Data store, or in NSData flat files in your application's Documents directory. You're correct that storing that in NSUserDefaults and then re parsing feels a little overkill.

我会将计算/解析的数据存储在核心数据存储中,或者存储在应用程序文档目录中的 NSData 平面文件中。您是正确的,将它存储在 NSUserDefaults 中然后重新解析感觉有点矫枉过正。

回答by Kyle

What type of data? If its text/string bases SQLLite would probably be the best.

什么类型的数据?如果它的文本/字符串基于 SQLLite 可能是最好的。