Nobik
Nobik

Reputation: 791

CFPreferences creates multiple files

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:

Upvotes: 4

Views: 678

Answers (2)

Sjoerd Perfors
Sjoerd Perfors

Reputation: 2317

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.

Upvotes: 1

Nikolai Ruhe
Nikolai Ruhe

Reputation: 81878

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?

Upvotes: 3

Related Questions