简体   繁体   English

NSUserDefaults或NSCache - 存储数据的最佳方式

[英]NSUserDefaults or NSCache - best way to store data

I'm building an IOS application, which will basically helps the users to see the orders he made on my website. 我正在构建一个IOS应用程序,它基本上可以帮助用户查看他在我的网站上发出的订单。

On the application startup, the list of his order reloads automatically. 在应用程序启动时,他的订单列表会自动重新加载。 What I'm trying to do here is to "save" this data into something to be able to show him his orders even if he's not connected to the internet. 我在这里要做的是将这些数据“保存”成能够向他显示他的订单,即使他没有连接到互联网。

I already used the NSUserDefaults class to save the credentials but I'm not sure this is the right way of saving more consistent data (which here consist of an NSArray of Order ). 我已经使用NSUserDefaults类来保存凭证,但我不确定这是保存更一致数据的正确方法(这里包含一个NSArrayOrder )。

I heard about the NSCache class which could also be a possibility, but I don't really which one would be the best in my case. 我听说过NSCache类也可能是一种可能性,但在我的情况下,我真的不是最好的。

Could someone give me some tips about it please ? 有人可以给我一些提示吗?

You are indeed doing the right thing by using NSUserDefaults . 您确实通过使用NSUserDefaults做了正确的事情。 You may convert your object to NSData if needed using NSKeyedArchiver/NSKeyedUnArchiver . 如果需要,可以使用NSKeyedArchiver/NSKeyedUnArchiver将对象转换为NSData Refer here: converting NSDictionary object to NSData object and vice-versa 请参考此处: 将NSDictionary对象转换为NSData对象,反之亦然

I think the best would be create a NSManagedObject custom class for your order class, and store the orders in core data. 我认为最好的方法是为您的订单类创建一个NSManagedObject自定义类,并将订单存储在核心数据中。 For details see this tutorial which is enough for solving this problem (and easy to understand the necessary basics of Core Data). 有关详细信息,请参阅本教程该教程足以解决此问题(并且易于理解核心数据的必要基础知识)。

Good luck! 祝好运!

CoreData is the best way for your app to store data. CoreData是您的应用程序存储数据的最佳方式。 Try using coredata. 尝试使用coredata。

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

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