gpt4 book ai didi

iphone - UserDefaults 线程安全吗

转载 作者:可可西里 更新时间:2023-11-01 03:35:23 27 4
gpt4 key购买 nike

我知道文档说它是,我知道这个主题已经在讨论中,但我有一个有趣的堆栈结果,我无法得出任何结论,但 [NSUserDefaults standardUserDefaults] 不是线程安全。所以我将发布我的崩溃日志并希望有人看到我看不到的东西......

线程崩溃

    ... 
libdispatch.dylib 0x3ab53d67 _dispatch_client_callout + 23
libdispatch.dylib 0x3ab65e73 _dispatch_barrier_sync_f_invoke + 27
CoreFoundation 0x302b470d CFPreferencesAppSynchronize + 265
Foundation 0x30151b01 -[NSUserDefaults(NSUserDefaults) synchronize] + 25
MyApp 0x0009df8b -[AppDelegate applicationDidEnterBackground:] (AppDelegate.m:178)
...
MyApp 0x0005344b main (main.m:17)
MyApp 0x000533f8 start + 40

其他话题

    ...
CoreFoundation 0x302bc13f _CFXPreferencesSetValue + 107
CoreFoundation 0x302bc039 CFPreferencesSetAppValue + 41
Foundation 0x30c76935 -[NSUserDefaults(NSUserDefaults) setObject:forKey:] + 61
MyApp 0x000b2e9d -[AppData parserDidEndDocument:] (AppData.m:1013)
...

部分堆栈已被删除并替换为“...”,因为它太长且与主题无关。应用程序崩溃并显示消息:

* Collection <__NSDictionaryM: 0x15a04ae0> was mutated while being enumerated. ....

代码:AppDelegate: -> 崩溃的线程...

    - (void)applicationDidEnterBackground:(UIApplication *)application
{
[[NSUserDefaults standardUserDefaults] synchronize];
}

MyClass: ->第二个线程:

    -(void)parserDidEndDocument:(NSXMLParser *)parser {
...
[[NSUserDefaults standardUserDefaults] setObject:arr forKey:@"savedStations"];
[[NSUserDefaults standardUserDefaults] setObject:[NSDate date] forKey:@"lastUpdateDate"];
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"alreadyLoaded"];
...
}

我在其他几个案例中遇到过这种崩溃,它总是包括在一个线程中写入用户默认值并在其他崩溃的线程中进行同步。如果有人有其他解释,我会非常高兴......

最佳答案

关于iphone - UserDefaults 线程安全吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19491036/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com