iOS:我应该使用Core Data还是NSUserDefaults? [英] iOS: Should I use Core Data or NSUserDefaults?

查看:122
本文介绍了iOS:我应该使用Core Data还是NSUserDefaults?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景:

我有一个应用程序可以从数据库中检索餐馆列表,每个餐馆都有各自的基本信息数组他们(小时,姓名,地址等)。我想先从应用程序的服务器上检索该信息,然后再使用NSUserDefaults或Core Data将其存储在应用程序本身中,因为该信息不太可能更改。我将要存储的餐厅的最大数量约为25,这是一个足够小的数据集合以使用NSUserDefaults吗?

I have an app that retrieves a list of restaurants from a database, each have an individual array of basic information about them (hours, name, address, etc). I would like to retrieve that information from the server on the apps first load, but then have it stored within the app itself, with either NSUserDefaults or Core Data, since the information is unlikely to change. The max number of restaurants I would be storing is about 25, is that a small enough data collection to use NSUserDefaults?

我在存储超过1,000条记录,但我只存储一个小数组。

I have looked at similar questions with storing data with over 1,000 records, but I am only storing a small array.

问题:

NSUserDefaults比Core Data更容易使用,因此,如果可能的话,我想避免使用Core Data。就我而言,如果我将餐厅列表而不是核心数据存储在NSUserDefaults中,会不会出现性能问题?

NSUserDefaults is much easier to use than Core Data, so if possible I would like to avoid using Core Data. In my case, will there be a performance problem if I am storing my list of restaurants in NSUserDefaults instead of Core Data?

推荐答案

取决于


  • 大小

  • 数据结构

  • 要求数据的完整性

仅由10或20个 restaruants组成的数组我肯定会存储在 NSUserDefaults 。但是只有当我确信这将永远不会变得更加复杂时。因为当您稍后扩展模型但从NSUserData开始时,您可能仍会使用 NSUserDefaults ,只是因为避免在已安装应用程序升级期间将其迁移到Core Data。

Just an Array of 10 or 20 "restaruants" I would certainly store in NSUserDefaults. But only when I am sure that this will never become more complex. Because when you later extend your model but started off with NSUserData then you may remain with NSUserDefaults just because you avoid migrating it to Core Data during the upgrade of an installed app.

因此,对于更复杂的结构(包括引用)以及当您对应用程序制定进一步计划以实现可能需要更多实体的更多功能时,则应该从一开始就选择Core Data。

So for more complex structures including references and when you have further plans with your app towards more functionality that may require more entities, then you should go for Core Data from start.

顺便说一句,它并不像您想象的那么复杂。

BTW, it is not that complicated as you may think.

但是,除了滥用NSUserDefaults外,您还可以使用 -writeToFile:atomically: -initWithContentsOfFile:读取它们。

However, instead of abusing NSUserDefaults, you could simply write an NSArray to file using -writeToFile:atomically: and -initWithContentsOfFile: to read them in.

这篇关于iOS:我应该使用Core Data还是NSUserDefaults?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆