我有以下功能:崩潰發生時嘗試設置同步或NSUserDefaults的
-(int) getCounter:(BOOL)isIncrease {
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSNumber *counter = [userDefaults valueForKey:kCounter];
int intCounter;
if (counter == nil){
intCounter = 0;
} else {
intCounter = [counter intValue];
}
if (isIncrease){
intCounter++;
[userDefaults setObject:[NSNumber numberWithInt:intCounter] forKey:kCounter];
[userDefaults synchronize];
}
return intCounter;
}
它看起來簡單明瞭。
不過有時我獲得以下的面料例外:
#11. Crashed: com.my.app
0 libobjc.A.dylib 0x1866def70 objc_msgSend + 16
1 CoreFoundation 0x187c1190c -[CFPrefsPlistSource synchronize] + 96
2 CoreFoundation 0x187c36674 -[CFPrefsSearchListSource alreadylocked_requestNewData] + 88
3 CoreFoundation 0x187cd80e8 __58-[_CFXPreferences appSynchronizeWithIdentifier:container:]_block_invoke + 36
4 CoreFoundation 0x187c38ea4 __108-[_CFXPreferences(SearchListAdditions) withSearchListForIdentifier:container:cloudConfigurationURL:perform:]_block_invoke + 268
5 CoreFoundation 0x187c3876c normalizeQuintuplet + 360
6 CoreFoundation 0x187c38d8c -[_CFXPreferences(SearchListAdditions) withSearchListForIdentifier:container:cloudConfigurationURL:perform:] + 104
7 CoreFoundation 0x187cd8058 -[_CFXPreferences appSynchronizeWithIdentifier:container:] + 292
8 Foundation 0x1886def90 -[NSUserDefaults(NSUserDefaults) synchronize] + 52
9 appApp 0x1004b11a4 -[MyProg getCounter:] + 4300083620
10 appApp 0x1004adb8c -[MyProg callServerWithEventName:value:withDictionary:isEvent:isNewAPI:] + 4300069772
11 appApp 0x1004ab658 __42-[MyProg sendEvent:withValues:]_block_invoke + 4300060248
12 libdispatch.dylib 0x186b1e1bc _dispatch_client_callout + 16
13 libdispatch.dylib 0x186b2af94 _dispatch_continuation_pop + 576
14 libdispatch.dylib 0x186b37634 _dispatch_source_latch_and_call + 204
15 libdispatch.dylib 0x186b20160 _dispatch_source_invoke + 820
16 libdispatch.dylib 0x186b2c210 _dispatch_queue_serial_drain + 468
17 libdispatch.dylib 0x186b219a4 _dispatch_queue_invoke + 652
18 libdispatch.dylib 0x186b2c8d8 _dispatch_queue_override_invoke + 360
19 libdispatch.dylib 0x186b2e34c _dispatch_root_queue_drain + 572
20 libdispatch.dylib 0x186b2e0ac _dispatch_worker_thread3 + 124
21 libsystem_pthread.dylib 0x186d272a0 _pthread_wqthread + 1288
22 libsystem_pthread.dylib 0x186d26d8c start_wqthread + 4
就以9號線一看:
9 appApp 0x1004b11a4 -[MyProg getCounter:] + 4300083620
有時它失敗的:[NSUserDefaults(NSUserDefaults) synchronize]
有時上:[NSUserDefaults(NSUserDefaults) setObject:forKey:]
有人可以解釋爲什麼我會發生這種事故,以及如何避免它?
我在我的代碼的很多地方使用UserDefaults
,但在上述函數中發生崩潰。
有什麼建議嗎?
[EDIT 1]
// in init:
mBackgroundQueue = dispatch_queue_create("com.my.app", NULL);
根函數我請getCounter
:
- (void) sendEvent{
dispatch_after(dispatch_time(DISPATCH_TIME_NOW,
(int64_t)(0.1f * NSEC_PER_SEC)),
mBackgroundQueue,
^{
[self callServerWithEventName:eventName ...
withDictionary:nil
isEvent:YES
isNewAPI:YES];
}
從callServerWithEventName
我打電話[self getCounter];
[EDIT 2]
崩潰發生在setObject:forKey:]
#14. Crashed: com.my.app
0 libdispatch.dylib 0x184d397bc dispatch_group_enter + 108
1 CoreFoundation 0x185e27610 -[CFPrefsPlistSource sendFullyPreparedMessage:toConnection:settingValue:forKey:retryCount:] + 240
2 CoreFoundation 0x185e27c48 -[CFPrefsPlistSource sendMessageSettingValue:forKey:] + 388
3 CoreFoundation 0x185e26620 -[CFPrefsPlistSource alreadylocked_setValues:forKeys:count:] + 864
4 CoreFoundation 0x185ee72a8 -[CFPrefsSource setValues:forKeys:count:removeValuesForKeys:count:] + 264
5 CoreFoundation 0x185e4dafc -[CFPrefsSearchListSource alreadylocked_setValues:forKeys:count:] + 428
6 CoreFoundation 0x185ee72a8 -[CFPrefsSource setValues:forKeys:count:removeValuesForKeys:count:] + 264
7 CoreFoundation 0x185ee7444 -[CFPrefsSource setValue:forKey:] + 60
8 CoreFoundation 0x185e505c0 __108-[_CFXPreferences(SearchListAdditions) withSearchListForIdentifier:container:cloudConfigurationURL:perform:]_block_invoke + 268
9 CoreFoundation 0x185e4fe88 normalizeQuintuplet + 360
10 CoreFoundation 0x185e504a8 -[_CFXPreferences(SearchListAdditions) withSearchListForIdentifier:container:cloudConfigurationURL:perform:] + 104
11 CoreFoundation 0x185eece7c -[_CFXPreferences setValue:forKey:appIdentifier:container:configurationURL:] + 276
12 Foundation 0x1868a4eb8 -[NSUserDefaults(NSUserDefaults) setObject:forKey:] + 68
13 ifyApp 0x1004a518c -[MyProg getCounter:] + 4300362124
14 ifyApp 0x1004a1b8c -[MyProg callServerWithEventName:value:withDictionary:isEvent:isNewAPI:] + 4300348300
15 ifyApp 0x10049f658 __42-[MyProg sendEvent:withValues:]_block_invoke + 4300338776
感謝,
'setObject:'是一個'NSUserDefaults'方法 – snaggs
是的,對不起,我忘了注意它已經在你的問題。你可以嘗試通過'objectForKey:'檢索值,並讓我們知道它是否仍然崩潰? – viral
@iOSAppDev我有1M的客戶,有時會發生這種情況。據我所知'NSUserDefaults'是一個單身人士,它真的很奇怪 – snaggs