NSUserDefaults errors in logs

后端 未结 6 980
慢半拍i
慢半拍i 2021-01-03 21:08

I get some error messages in logs

[User Defaults] Failed to write value for key GameId in CFPrefsPlistSource<0x1740faf00> (Domain: xxx.xxxxxx, User

6条回答
  •  攒了一身酷
    2021-01-03 21:54

    I'd like to share my solution for this problem. For my case, the error was a legitimate error and my data was not saved properly. It turns out the error was caused by having a NSUserDefaults "data saving" in a loop.

    By "data saving" I mean this (pseudocode):

    loop{
        [defaults setObject:@"Data" forKey:@"Key"];
        [defaults synchronize];
    }
    

    So the code executes this multiple times very fast and cause the problem. I guess "synchronize" cannot handle concurrent calls.

    What needs to be done is:

    loop{
        [defaults setObject:@"Data" forKey:@"Key"];
    }
    [defaults synchronize];
    

    synchronize be called once after all the objects been set.

提交回复
热议问题