Crash in NSUserDefaults when setObject:forKey: in background thread is called

I´m doing some stuff in the background (Networking, Core Data, ...).

There is also the following line called in some methods:


    [[NSUserDefaults standardUserDefaults] setObject:[NSDate date] forKey:@"SyncDate"];


Everytime, except the first time, when this line is called, it will crash. but only in a background thread.

Here is my stack trace:


Thread : Crashed: com.apple.root.background-qos
0  libobjc.A.dylib                0x0000000199b0dbd0 objc_msgSend + 16
1  Foundation                     0x0000000185ea1eb4 -[NSObject(NSKeyValueObserverNotification) willChangeValueForKey:] + 324
2  CoreFoundation                 0x0000000185090994 _CFPreferencesSetValueWithContainer + 168
3  Foundation                     0x0000000185eb9138 -[NSUserDefaults(NSUserDefaults) setObject:forKey:] + 56
4  AIR                            0x00000001000ebd70 -[ServerSyncAgent syncUnsyncedEntities]
5  AIR                            0x0000000100145568 __37-[StreamListener stream:handleEvent:]_block_invoke88
6  libdispatch.dylib              0x000000019a2e97b0 _dispatch_call_block_and_release + 24
7  libdispatch.dylib              0x000000019a2e9770 _dispatch_client_callout + 16
8  libdispatch.dylib              0x000000019a2f7bb0 _dispatch_root_queue_drain + 2140
9  libdispatch.dylib              0x000000019a2f734c _dispatch_worker_thread3 + 112
10 libsystem_pthread.dylib        0x000000019a4fd478 _pthread_wqthread + 1092


I do not observe NSUserDefaults with KVO in any place and NSUserDefaults is thread safe.

Any ideas?

That’s quite a strange crash you have there.

Is this OS X or iOS?

How reproducible is this? If it’s reasonably reproducible, try this as an experiment: early in your boot sequence, call

+[NSUserDefaults standardUserDefaults]
and save that reference (in a global variable (assuming ARC), or a
strong
ivar of your app delegate, or anywhere else that lives for the duration of your app).

Share and Enjoy

Quinn "The Eskimo!"
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

I've seen this too and saving the standardUserDefaults in variable doesn't help. I've been able to reproduce this quite frequently in the Simulator. NSUserDefaults is used quite extensively in our app and usually the app writes some values received from the server to the user defaults (Uses AFNetworking). All calls happen in the main thread. Example


[[AFHTTPRequestOperationManager manager] POST:url parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
    [[NSUserDefaults standardUserDefaults] setObject:@"firstname.lastname@something.com" forKey:@"email"];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
   
}];

Seems it doesn't crash if I use NSUserDefaults -initWithSuiteName instead of NSUserDefaults +standardUserDefaults.

Can it really avoid the crash ?

Crash in NSUserDefaults when setObject:forKey: in background thread is called
 
 
Q