2010-03-18 48 views
4

我只有一个小问题:CFPreferences创建多个文件

为什么CFPreferences-API在我的用户偏好设置,目录中创建多个文件?所有文件都捆绑我标识符名称和所有的(除了一个,原来的)添加一个后缀是这样的:

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

回答

3

这看起来非常像原子写作的副作用。

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

有趣的命名文件看起来像是来自此进程的工件。也许你的应用程序在原子写入时崩溃了?

0

如果例如关闭应用程序时同步:

- (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的图像。