美文网首页
NSUserDefaults的同步问题

NSUserDefaults的同步问题

作者: frankisbaby | 来源:发表于2018-07-12 15:30 被阅读109次

    - (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会同步线程,直到所有的操作结束;

    相关文章

      网友评论

          本文标题:NSUserDefaults的同步问题

          本文链接:https://www.haomeiwen.com/subject/zlacpftx.html