为什么NSUserDefaults会在我的应用程序的库/首选项中留下临时plist文件?

bsn*_*eed 7 macos cocoa objective-c nsuserdefaults

我无法弄清楚为什么NSUserDefaults在我的应用程序的Library/Preferences中留下垃圾plist文件.

我看到以下文件......

com.mycompany.myapp.plist
com.mycompany.myapp.plist.3gaPYul
com.mycompany.myapp.plist.c97yxEH
Run Code Online (Sandbox Code Playgroud)

...等plist.*文件是0字节.似乎每次运行应用程序时,它都会留下一个新的.我确定我根本不打电话-[NSUserDefaults synchronize],但是如果我确实打电话,它会加速给定跑步的垃圾文件外观.在调试器中单步执行,一旦我跳过同步调用,就会出现一个新文件.如果我取出同步调用,有时候应用程序启动时会出现一个新的垃圾文件,其他时候会在应用程序退出时出现.

我也在检查是否可能在线程上设置用户默认值(不太可能,但也许是可能),认为文档说它是线程安全的.

任何帮助表示赞赏.谢谢!

编辑:

刚刚发现:CFPreferences创建了多个文件

虽然我同意回答者的想法,但它没有解释"为什么?" 部分.

bsn*_*eed 3

我已经确信这是苹果的一个错误,但我无法制作一个小样本来说明它。我收到了大量反馈说苹果自己的应用程序可以做到这一点。由于我碰壁了并且需要继续前进,所以我最终做了如下所示的令人讨厌的黑客攻击。

@implementation NSUserDefaults(Hack)

- (BOOL)synchronize
{
BOOL result = CFPreferencesAppSynchronize((CFStringRef)[[NSBundle mainBundle] bundleIdentifier]);
if (!result)
{
    // there's probably a temp file lingering around... try again.
    result = CFPreferencesAppSynchronize((CFStringRef)[[NSBundle mainBundle] bundleIdentifier]);

    // regardless of the result, lets clean up any temp files hanging around..
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *prefsDir = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Preferences"];
    NSDirectoryEnumerator *dirEnumerator = [fileManager enumeratorAtPath:prefsDir];
    NSString *file = nil;
    NSString *match = [[[NSBundle mainBundle] bundleIdentifier] stringByAppendingString:@".plist."];
    while ((file = [dirEnumerator nextObject]))
    {
        if ([file rangeOfString:match].location != NSNotFound)
        {
            NSString *fileToRemove = [prefsDir stringByAppendingPathComponent:file];
            [fileManager removeItemAtPath:fileToRemove error:nil];
        }
    }
}

return result;
}
Run Code Online (Sandbox Code Playgroud)