NSUserDefaults Crash

When I save massive data by NSUserDefaults (convert a lot of images into data), it take a long time, so i kill the app, and the strange things happened. When i reopen the app, it take the 'watchdog', and then crash. I got the crash log and find it is the NSUserDefaults problem.


This is the log:


Exception Type: EXC_CRASH (SIGKILL)

Exception Codes: 0x0000000000000000, 0x0000000000000000

Exception Note: EXC_CORPSE_NOTIFY

Termination Reason: Namespace SPRINGBOARD, Code 0x8badf00d

Termination Description: SPRINGBOARD, scene-update watchdog transgression: com.das8848.www.TechnicianApp123 exhausted real (wall clock) time allowance of 10.00 seconds | ProcessVisibility: Background | ProcessState: Running | WatchdogEvent: scene-update | WatchdogVisibility: Foreground | WatchdogCPUStatistics: ( | "Elapsed total CPU time (seconds): 0.200 (user 0.200, system 0.000), 30% CPU", | "Elapsed application CPU time (seconds): 0.001, 0% CPU" | )

Triggered by Thread: 0



Thread 0 name: Dispatch queue: com.apple.main-thread

Thread 0 Crashed:

0 libsystem_kernel.dylib 0x00000001e64ff9fc __ulock_wait + 8

1 libsystem_platform.dylib 0x00000001e657334c _os_unfair_lock_lock_slow + 220

2 CoreFoundation 0x00000001e6852d7c -[CFPrefsSource copyValueForKey:] + 40

3 CoreFoundation 0x00000001e69a37dc __76-[_CFXPreferences copyAppValueForKey:identifier:container:configurationURL:]_block_invoke + 40

4 CoreFoundation 0x00000001e68e4908 __108-[_CFXPreferences+ 620808 (SearchListAdditions) withSearchListForIdentifier:container:cloudConfigurationURL:perform:]_block_invoke + 268

5 CoreFoundation 0x00000001e68e425c normalizeQuintuplet + 356

6 CoreFoundation 0x00000001e6850db4 -[_CFXPreferences+ 15796 (SearchListAdditions) withSearchListForIdentifier:container:cloudConfigurationURL:perform:] + 108

7 CoreFoundation 0x00000001e6851644 -[_CFXPreferences copyAppValueForKey:identifier:container:configurationURL:] + 152

8 CoreFoundation 0x00000001e69a609c _CFPreferencesCopyAppValueWithContainerAndConfiguration + 124

9 Foundation 0x00000001e72e840c -[NSUserDefaults+ 29708 (NSUserDefaults) objectForKey:] + 52

10 TechnicianApp 0x0000000102332f68 -[DAInspectViewController getLocalData:workBaseId:] + 1240936 (DAInspectViewController.m:204)


the code on DAInspectViewController.m:204 is:

NSData *arrData = [[NSUserDefaults standardUserDefaults]valueForKey:key];


So I want to know what wrong i have about this code.

Thanks.

Accepted Reply

You have probably reached the limits of outer space (I mean NSUserDefaults), which is not intended to save massive data, AFAIK.

You trigger a time out after 10s (intended to avoid some program block the device indefinetely).

Replies

You have probably reached the limits of outer space (I mean NSUserDefaults), which is not intended to save massive data, AFAIK.

You trigger a time out after 10s (intended to avoid some program block the device indefinetely).

Maybe. I will change database to do it. it is not so suitable to Use NSUserDefaults to save masstive data. Not only crash, but also cost performance.😕

I use these, they can be modified to read/write NSData:


-(void)writeAnArray:(NSArray *)array atPath:(NSString *)path{ 
    NSFileManager* sharedFM = [NSFileManager defaultManager]; 
    NSURL *appDir=[[sharedFM URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] objectAtIndex:0]; 
    NSURL *fileURL=[appDir URLByAppendingPathComponent:path]; 
    [array writeToURL:fileURL error:nil]; 
}



-(NSArray *)readAnArray:(NSString *)path{
    NSFileManager* sharedFM = [NSFileManager defaultManager];
    NSURL *appDir=[[sharedFM URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] objectAtIndex:0];
    NSURL *fileURL=[appDir URLByAppendingPathComponent:path];
    NSError *error;
    NSArray *fileArray=[NSArray arrayWithContentsOfURL:fileURL error:&error];
    if(error)fileArray=[[NSArray alloc] init];
    return fileArray;
}