问题
Ok, so I launched an app last week before iOS8 came out. Everything was working fine in iOS7 and below. But now since people have updated to iOS8 my app is pausing/crashing for no reason.
I came to terms that it is when I set an NSMutableArray to the NSUserDefaults, it pauses.
Please note, the NSMutableArray is an array of NSStrings.
BUT (this is weird) if I breakpoint skip through the code it works and I get no pause.
Here is the function that it is blowing up...
-(void)UpdateMyAgenda:(NSString*)SessionID Remove:(BOOL)Remove{
if(!Remove){
//Do not Remove
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
_MyAgenda = [[NSMutableArray alloc]init];
_MyAgenda = [prefs mutableArrayValueForKey:@"MyAgenda"];
[_MyAgenda addObject:SessionID];
[prefs setObject:_MyAgenda forKey:@"MyAgenda"];
[prefs synchronize];
}else{
//Remove
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
_MyAgenda = [[NSMutableArray alloc]init];
_MyAgenda = [prefs mutableArrayValueForKey:@"MyAgenda"];
[_MyAgenda removeObject:SessionID];
[prefs setObject:_MyAgenda forKey:@"MyAgenda"];
[prefs synchronize];
}
for (NSString *item in _MyAgenda) {
NSLog(@"%@", item);
}
NSLog(@"-----------");
}
回答1:
Ok so here is the deal, or a fix for now. It looks like setting a mutable array in the player prefs is breaking, but setting an NSArray is not.
So convert your array to a mutable one, add the object, then convert your mutable array to an nsarray and then set the user default.
Here is an example....
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSArray *arr = [prefs arrayForKey:@"MyAgenda"];
_MyAgenda = [NSMutableArray arrayWithArray:arr];
[_MyAgenda addObject:SessionID];
arr = [NSArray arrayWithArray:_MyAgenda];
[prefs setObject:arr forKey:@"MyAgenda"];
[prefs synchronize];
来源:https://stackoverflow.com/questions/26029704/nsuserdefaults-pause-crash-on-ios8