Reputation: 14632
Is it possible to store settings on the iPhone immediately? The apps on iOS 4+ don't close on exit, and if I press 'Stop process' in Xcode settings are not saved? How to save them?
- (void)compLoad {
compTotal = [[NSUserDefaults standardUserDefaults] integerForKey: @"compTotal"];
compCount = [[NSUserDefaults standardUserDefaults] integerForKey: @"compCount"];
compShow = [[NSUserDefaults standardUserDefaults] boolForKey: @"compShow"];
}
- (void)compSave {
[[NSUserDefaults standardUserDefaults] setInteger: compTotal forKey: @"compTotal"];
[[NSUserDefaults standardUserDefaults] setInteger: compCount forKey: @"compCount"];
[[NSUserDefaults standardUserDefaults] setBool: compShow forKey: @"compShow"];
}
Upvotes: 2
Views: 5299
Reputation: 1206
[[NSUserDefaults standardUserDefaults] synchronize];
synchronize - this method is automatically invoked at periodic intervals, use this method only if you cannot wait for the automatic synchronization (for example, if your application is about to exit) or if you want to update the user defaults to what is on disk even though you have not made any changes.
Upvotes: 9
Reputation: 11174
According to the documentation, the following call will persist any outstanding modifications. To check if this has been successful, check the return value of this call. A value of YES indicates success.
[[NSUserDefaults standardUserDefaults] synchronize];
Upvotes: 6
Reputation: 92384
Simply call [[NSUserDefaults standardUserDefaults] synchronize];
. But usually you do not need to that: iOS will save once your app goes into background and also on a few other occasions. Calling synchronize
too often is a considered to be a Bad Thing(tm).
Upvotes: 2
Reputation: 6692
Try using the synchronize
method.
Update: you should consider registering your defaults, like it suggests in the Preferences and Settings Programming Guide:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Register the preference defaults early.
NSDictionary *appDefaults = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:@"CacheDataAgressively"];
[[NSUserDefaults standardUserDefaults] registerDefaults:appDefaults];
// Other initialization...
}
Also, from this guide ("Synchronizing and Detecting Preference Changes"), regarding syncing:
To detect when changes to a preference value occur, apps can also register for the notification NSUserDefaultsDidChangeNotification. The shared NSUserDefaults object sends this notification to your app whenever it detects a change to a preference located in one of the persistent domains. You can use this notification to respond to changes that might impact your user interface. For example, you could use it to detect changes to the user’s preferred language and update your app content appropriately.
Pretty sure you still need to use synchronize
if you want to re-read settings directly.
Upvotes: 1
Reputation: 4131
Call your compSave
in your AppDelegate
- (void)applicationWillResignActive:(UIApplication *)application
{
// Call your compSave here to force saving before app is going to the background
}
also
- (void)compSave
{
[[NSUserDefaults standardUserDefaults] setInteger: compTotal forKey: @"compTotal"];
[[NSUserDefaults standardUserDefaults] setInteger: compCount forKey: @"compCount"];
[[NSUserDefaults standardUserDefaults] setBool: compShow forKey: @"compShow"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
Upvotes: 3