CFPreferences creates multiple files

廉价感情. 提交于 2019-12-07 08:00:39

问题


I have only a little question:

Why the CFPreferences-API creates multiple files in my UserPrefs-Directory? All the files have my Bundle-Identifier as name and all (except the one, the original) have added a suffix like this:

  • com.myComp.myApp.plist <- (only this plist-file should be created)
  • com.myComp.myApp.plist.0qzcicc
  • com.myComp.myApp.plist.8dhjfht

回答1:


This looks very much like a side effect of atomic writing.

Atomic writing means that, whenever a file is to be written from an NSData (or other) object, the file is first created using a temporary file name in the same directory. Then all data is written into that file (an operation which is usually not atomic). After closing the file it is renamed to the original file name. The rename is an atomic step, which ensures that any other process that might look at the file sees either the complete old file or the complete new file. There’s no way that a process might see only half of a file.

The funny named files look like they are artifacts from this process. Maybe your app crashed in the middle of an atomic write?




回答2:


If you do synchronize when shutting down the app for example:

- (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.
}

It will try to write to dummy file first and do a atomic rename after. If the writing takes to long you will end up with a dummy file.

In my case I had some users with 14mb plists and end up having a lot of dummy files (taking almost 2G).

My problem and fix was to compress a image which I wrote to the userdefaults.



来源:https://stackoverflow.com/questions/2468403/cfpreferences-creates-multiple-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!