Background:
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?
我有一个应用程序从数据库中检索餐馆列表,每个应用程序都有一个单独的基本信息数组(小时,名称,地址等)。我想首先从应用程序服务器上检索该信息,然后将其存储在应用程序本身中,使用NSUserDefaults或Core Data,因为信息不太可能发生变化。我要存储的最大餐厅数量大约是25,这是一个足够小的数据集来使用NSUserDefaults吗?
I have looked at similar questions with storing data with over 1,000 records, but I am only storing a small array.
我已经查看了类似的问题,存储了超过1,000条记录的数据,但我只存储了一个小数组。
Question:
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?
NSUserDefaults比Core Data更容易使用,所以如果可能的话我想避免使用Core Data。在我的情况下,如果我在NSUserDefaults而不是Core Data中存储我的餐馆列表,是否会出现性能问题?
3 个解决方案
#1
4
Depends on the
取决于
- size
- structure of data
- requirements re integrity of the 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.
只是一个包含10或20个“restaruants”的数组,我肯定会存储在NSUserDefaults中。但只有当我确信这永远不会变得更加复杂时。因为当您稍后扩展模型但从NSUserData开始时,您可能会继续使用NSUserDefaults,因为您在升级已安装的应用程序期间避免将其迁移到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.
因此,对于包括引用在内的更复杂的结构,以及当您对应用程序进一步规划可能需要更多实体的更多功能时,您应该从头开始使用Core Data。
BTW, it is not that complicated as you may think.
顺便说一句,它并不像你想象的那么复杂。
However, instead of abusing NSUserDefaults, you could simply write an NSArray to file using -writeToFile:atomically:
and -initWithContentsOfFile:
to read them in.
但是,您可以使用-writeToFile:atomically:和-initWithContentsOfFile:将NSArray写入文件,而不是滥用NSUserDefaults。
#2
1
NSUserDefaults
is not intended for data, it's for storing simple key-value pairs like user settings and flags. Core Data is a bit hard to learn at first, but definitely worth it, even for simple applications.
NSUserDefaults不用于数据,它用于存储简单的键值对,如用户设置和标志。核心数据起初有点难学,但绝对值得,即使对于简单的应用程序也是如此。
If it really is a small, simple data set that won't change very often, you can also store some data in a local plist (i.e. save NSArray
or NSDictionary
to plist using writeToFile
method). This isn't very different from using NSUserDefaults
in terms of performance, although I think it's cleaner and easier to manage. If it never changes you can also include the plist with your app resources in XCode by creating a plist file and filling it in with your data.
如果它确实是一个不会经常更改的小而简单的数据集,您还可以将一些数据存储在本地plist中(即使用writeToFile方法将NSArray或NSDictionary保存为plist)。这与使用NSUserDefaults在性能方面没有太大差别,尽管我认为它更干净,更易于管理。如果它永远不会改变,您还可以通过创建plist文件并将数据填入XCode,将plist与您的应用资源包含在XCode中。
#3
1
Considering the amount of the data, user default or a specific plist/json file are all good. CoreData is definitely overkilling.
考虑到数据量,用户默认或特定的plist / json文件都很好。 CoreData肯定是过度的。
#1
4
Depends on the
取决于
- size
- structure of data
- requirements re integrity of the 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.
只是一个包含10或20个“restaruants”的数组,我肯定会存储在NSUserDefaults中。但只有当我确信这永远不会变得更加复杂时。因为当您稍后扩展模型但从NSUserData开始时,您可能会继续使用NSUserDefaults,因为您在升级已安装的应用程序期间避免将其迁移到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.
因此,对于包括引用在内的更复杂的结构,以及当您对应用程序进一步规划可能需要更多实体的更多功能时,您应该从头开始使用Core Data。
BTW, it is not that complicated as you may think.
顺便说一句,它并不像你想象的那么复杂。
However, instead of abusing NSUserDefaults, you could simply write an NSArray to file using -writeToFile:atomically:
and -initWithContentsOfFile:
to read them in.
但是,您可以使用-writeToFile:atomically:和-initWithContentsOfFile:将NSArray写入文件,而不是滥用NSUserDefaults。
#2
1
NSUserDefaults
is not intended for data, it's for storing simple key-value pairs like user settings and flags. Core Data is a bit hard to learn at first, but definitely worth it, even for simple applications.
NSUserDefaults不用于数据,它用于存储简单的键值对,如用户设置和标志。核心数据起初有点难学,但绝对值得,即使对于简单的应用程序也是如此。
If it really is a small, simple data set that won't change very often, you can also store some data in a local plist (i.e. save NSArray
or NSDictionary
to plist using writeToFile
method). This isn't very different from using NSUserDefaults
in terms of performance, although I think it's cleaner and easier to manage. If it never changes you can also include the plist with your app resources in XCode by creating a plist file and filling it in with your data.
如果它确实是一个不会经常更改的小而简单的数据集,您还可以将一些数据存储在本地plist中(即使用writeToFile方法将NSArray或NSDictionary保存为plist)。这与使用NSUserDefaults在性能方面没有太大差别,尽管我认为它更干净,更易于管理。如果它永远不会改变,您还可以通过创建plist文件并将数据填入XCode,将plist与您的应用资源包含在XCode中。
#3
1
Considering the amount of the data, user default or a specific plist/json file are all good. CoreData is definitely overkilling.
考虑到数据量,用户默认或特定的plist / json文件都很好。 CoreData肯定是过度的。