NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:@"xxxxxxxx" forKey:@"name"];
[defaults synchronize];
I need to know why do i have to use the last line of the above code [defaults synchronize] ? What is the purpose of using it? Is it a must ?
The purpose of
[default synchronize];is to make the user defaults get written on disk immediately. You don’t need to call it explicitly, iOS already does it at appropriate moments. So you can remove that line. In fact, it’s a performance problem if you callsynchronizeevery time you set a default.Prior to iOS 7, the user defaults were always synchronized when the application transitioned into background. As of iOS 7, that is no longer the case, so you might want to call
synchronizein your app delegate’sapplicationDidEnterBackground:or register to theUIApplicationDidEnterBackgroundNotificationnotification to do that.From the documentation for
-[NSUserDefaults synchronize]: