简体   繁体   English

NSArray或NSDictionary用于存储在UserDefaults中?

[英]NSArray or NSDictionary for storing in UserDefaults?

I've a collection of a usertype News currently stored in a NSArray. 我有一个当前存储在NSArray中的用户类型“新闻”的集合。 I have to store them in UserDefaults to save them on the Device. 我必须将它们存储在UserDefaults中才能将它们保存在设备上。 A News have a property ID which is unique. 新闻具有唯一的属性ID。 Before storing them to UserDefaults, I convert the NSArray to a NSDictionary where the key is the ID and the object for this key is the News. 在将它们存储到UserDefaults之前,我将NSArray转换为NSDictionary,其中的键是ID,此键的对象是News。 Is this a good way to store them? 这是存储它们的好方法吗?

The reason for choosing an NSDictionary is to be able to check if a News with a specific ID exists in the list of News each time I redownload the News from a Web Service. 选择NSDictionary的原因是,每次我从Web服务重新下载新闻时,都能够检查新闻列表中是否存在具有特定ID的新闻。 This way I have only one News instance for each News ID. 这样,每个新闻ID只能有一个新闻实例。 I think this would be hard to manage using a normal NSArray -- or I'm missing something? 我认为使用普通的NSArray很难管理-否则我会丢失某些东西?

I have to store them in UserDefaults to save them on the Device. 我必须将它们存储在UserDefaults中才能将它们保存在设备上。

UserDefaults are for storing preferences, settings and other "small" bit of information, they are not for storing application data. UserDefaults用于存储首选项,设置和其他“小”信息,而不用于存储应用程序数据。 You do not "have" to use NSUserDefaults, there are many other options available to you. 您没有“拥有”使用NSUserDefaults的权利,您可以使用许多其他选项。 Writing to files (in the document directory) in their original form or using NSKeyedArchiver/NSKeyedUnarchiver for example. 以原始格式(例如,使用NSKeyedArchiver / NSKeyedUnarchiver)写入文件(在文档目录中)。

You can get the document directory path like this 您可以像这样获取文档目录路径

NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

Apart from that storing an NSDictionary with News ids as keys is fine. 除了存储以新闻ID作为密钥的NSDictionary之外,还可以。 I don't really see the point of having an NSArray and "converting" it to an NSDictionary, use one or the other (that way you always ensure that you only have one News per item). 我真的没有看到拥有NSArray并将其“转换”为NSDictionary的意义,而是使用其中的一种(这样一来,您始终可以确保每个项目只有一个News)。

NSUserDefaults is not a database, but you already knew it. NSUserDefaults不是数据库,但是您已经知道它。

Anyway, I'd store the array directly and also save the last seen news ID as a separate preference . 无论如何,我将直接存储数组,还将最后一次看到的新闻ID保存为单独的首选项 Then, you only need to process news items with ID greater than the last seen item. 然后,您只需要处理ID大于上次看到的项目的新闻项目。 When you download news items you need to update both the news and the last seen ID. 下载新闻项时,您需要同时更新新闻和上次看到的ID。 You need to guarantee that IDs are in ascending order. 您需要确保ID升序。

A different approach, depending on your particular use of news, would be to save two arrays, one with the news items and a different one with the list of news IDs. 根据您对新闻的特殊使用,一种不同的方法是保存两个数组,一个数组存储新闻项,另一个数组存储新闻ID列表。 Use the second array to check if there are fresh news. 使用第二个数组检查是否有新消息。

Unless you are handling lot of news items (in which case you should use Core Data anyway) you are optimizing something that doesn't need optimization. 除非您要处理大量新闻项目(在这种情况下,无论如何都应使用Core Data),否则您正在优化不需要优化的内容。 Reading, sorting, updating and saving back a small array into NSUserDefaults is pretty fast. 读取,排序,更新和将小数组保存回NSUserDefaults的速度非常快。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM