4

我只有一个小问题:

为什么 CFPreferences-API 在我的 UserPrefs-Directory 中创建多个文件?所有文件都以我的 Bundle-Identifier 作为名称,并且所有文件(除了一个,原始文件)都添加了这样的后缀:

  • com.myComp.myApp.plist <- (仅应创建此 plist 文件)
  • com.myComp.myApp.plist.0qzcicc
  • com.myComp.myApp.plist.8dhjfht
4

2 回答 2

3

这看起来很像原子写入的副作用。

原子写入意味着,每当要从NSData(或其他)对象写入文件时,首先使用同一目录中的临时文件名创建文件。然后将所有数据写入该文件(通常不是原子操作)。关闭文件后,它被重命名为原始文件名。重命名是一个原子步骤,它确保可能查看文件的任何其他进程看到完整的旧文件或完整的新文件。进程不可能只看到文件的一半。

有趣的命名文件看起来像是这个过程的产物。也许您的应用程序在原子写入过程中崩溃了?

于 2010-03-18T09:00:12.273 回答
1

如果您在关闭应用程序时进行同步,例如:

- (void)applicationWillResignActive:(UIApplication *)application
{
    [[NSUserDefaults standardUserDefaults] synchronize];
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

它会先尝试写入虚拟文件,然后再进行原子重命名。如果写入需要很长时间,您最终会得到一个虚拟文件。

就我而言,我有一些用户使用 14mb plists,最终有很多虚拟文件(占用将近 2G)。

我的问题和解决方法是压缩我写给 userdefaults 的图像。

于 2015-03-16T15:10:08.283 回答