2010-03-18 47 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的圖像。