NSUserDefaults “Pause” / Crash on iOS8

一个人想着一个人 提交于 2019-12-08 08:48:59

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!