iOS: Data Persistence

125 篇文章 1 订阅

有以下几种Data Persistence的方式:

1. 自定义一个plist file来存储。如果你只是存储一些简单的array, dictionary等,则使用这种方式。 plist file是存在<app root>/Document

2. 使用NSUserDefaults来存储。 顾名思义,NSUserDefaults通常是用来存储user settings and preferences. data保存在<app root>/Library/Preferences folder下。NSUserDefaults是cache机制,内存数据会自动和database里的东西同步,而不会想plist那样要手动存储。也就是说,但你调用NSUserDefaults来set data时,就已经存到database里了,但最保险是调用sync方法。而对于plist来说,你要手动把data写入plist file里。这样我们就可以设置在resign app时才把data存到plist里。

NSUserDefaults is not used for storing collections of data.

是否需要担心NSUserDefaults的performance? 答案是不需要,见http://stackoverflow.com/questions/6114641/accessing-nsuserdefaults-often


3. SQLite存储:主要用于存储relational data。

4. Core Data存储。如果要存储一些简单的data,那么使用core data就太大材小用了。core data是应该用来存储complex object。

5. 另外还有一个NSKeyedArchiver,保存整个这个对象数据, 很像java中的对象整体序列化。 见http://stackoverflow.com/questions/4555025/nskeyedarchiver-or-nsarray-writetofileatomically and http://blog.csdn.net/nono_love_lilith/article/details/7539659

6. When storing usernames and passwords it is better to use the Keychain service available in the iOS.   (Keychain Services Programming Guide in the Apple Documentation)


ref links

http://mobile.tutsplus.com/tutorials/iphone/iphone-sdk_store-data/  (best)

http://stackoverflow.com/questions/7058858/should-i-use-nsuserdefaults-or-a-plist-to-store-data

http://blog.csdn.net/nono_love_lilith/article/details/7539659

http://blog.csdn.net/liuhongwei123888/article/details/6841338

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值