Is NSUserDefault thread safe?

前端 未结 2 1891
忘掉有多难
忘掉有多难 2021-01-31 15:40

https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSUserDefaults_Class/Reference/Reference.html says it is

...shou

2条回答
  •  北海茫月
    2021-01-31 16:29

    Speaking for 10.10 and iOS8 if you looking into the implementation you'll find that -[NSUserDefaults setObject:forKey:] is calling __CFPreferencesSetAppValueWithContainer, which will eventually end up in +[CFPrefsSource withSourceForIdentifier:user:byHost:container:perform:]. This method is using a pthread_mutex_t to lock the access to the dictionary containing the values.

    So NSUserDefaults is thread safe.

提交回复
热议问题