NSUserDefaults的同步问题

- (nullableid)objectForKey:(NSString*)defaultName

/*!

 -objectForKey: will search the receiver's search list for a default with the key 'defaultName' and return it. If another process has changed defaults in the search list, NSUserDefaults will automatically update to the latest values. If the key in question has been marked as ubiquitous via a Defaults Configuration File, the latest value may not be immediately available, and the registered value will be returned instead.

 */

意思是:这个方法会搜寻列表,然后查找值来返回。如果另一个进程已经改变了默认值,NSUserDefaults将会自动更新最新的值。如果这个key已经在默认的配置文件中被标记,最近的值可能不会立即可以使用,注册的值会被返回。

- (void)setObject:(nullableid)value forKey:(NSString*)defaultName;

/*!

 -setObject:forKey: immediately stores a value (or removes the value if nil is passed as the value) for the provided key in the search list entry for the receiver's suite name in the current user and any host, then asynchronously stores the value persistently, where it is made available to other processes.

 */

立即存储这个值到沙盒,异步持久化存储这个值,它可以用于其他进程。

鉴于NSUserDefaults存在的不同步问题,对于在沙盒中的数据不能立即使用;

- (BOOL)synchronize;

/*!

 -synchronize is deprecated and will be marked with the NS_DEPRECATED macro in a future release.

 -synchronize blocks the calling thread until all in-progress set operations have completed. This is no longer necessary. Replacements for previous uses of -synchronize depend on what the intent of calling synchronize was. If you synchronized...

 - ...before reading in order to fetch updated values: remove the synchronize call

 - ...after writing in order to notify another program to read: the other program can use KVO to observe the default without needing to notify

 - ...before exiting in a non-app (command line tool, agent, or daemon) process: call CFPreferencesAppSynchronize(kCFPreferencesCurrentApplication)

 - ...for any other reason: remove the synchronize call

 */

synchronize会同步线程,直到所有的操作结束;

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,424评论 0 10
  • 【翻译全文】 Google in 'constant dialogue' with Beijing, Schmid...
    叶妖阅读 144评论 0 0
  • 我的爹是个沉默但是又有点闷骚的人,时不时的跟我开些不伤大雅的小玩笑。每当这时候,那双小眼睛就会眯到一起,一边嘚瑟,...
    bnngoo冰狗阅读 340评论 0 0
  • ——今日悦读—— 【0212今日悦读】君 【书名】《看见》 【作者】柴静 【金句】“先要把农民社保这条路铺平,无论...
    君暖2018阅读 127评论 0 0
  • 从来不会因为任何人的存在而放弃梦想 从来不会因为任何人的眼光而放弃方向 从来不会因为任何人的改变而放弃坚持 从来不...
    鐘玥阅读 400评论 2 9