How to run the code only once?

I am working on an iPhone application, and I am wondering if I can run a certain segment of code only once (in other words: the initialization code, which I want it to be executed only the first time), Here is my code that I execute in the method didFinishLaunchingwithOptions :

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. // Add the tab bar controller view to the window and display. [self.window addSubview:tabBarController.view]; [self.tabBarController setSelectedIndex:2]; [self.window makeKeyAndVisible]; [self createPlist1]; [self createPlist2]; [self createPlist3]; return YES; 

}

I want the last three messages to be executed only on the first start. I thought I could use UserDefaults and set the key after these messages were executed (at the first start) and check the value of this key at every start, but I feel that there is a better idea that I do not know.

Thanks in advance.

+4
source share
6 answers

Using customization (via NSUserDefaults ) is how this is usually done. For added value, set the value to "latest version"; Thus, you will be able to run the code not only once for each application, but once to update the version.

However, your startup code has persistent side effects, right? These plates go somewhere. Therefore, you can check if they exist before they are created. Use the result of the startup code as a trigger to start it again.

EDIT:

 NSUserDefaults *Def = [NSUserDefaults standardUserDefaults]; NSString *Ver = [Def stringForKey:@"Version"]; NSString *CurVer = [[[NSBundle mainBundle] infoDictionary] objectForKey:(NSString*)kCFBundleVersionKey]; if(Ver == nil || [Ver compare:CurVer] != 0) { if(Ver == nil) { //Run once per lifetime code } //Run once-per-upgrade code, if any [Def setObject:CurVer forKey:@"Version"]; } 
+11
source

A simpler solution →

 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; if ([defaults objectForKey:@"FirstTimeBool"]==nil) { [defaults setObject:@"YES" forKey:@"FirstTimeBool"]; ... //Code to be executed only once until user deletes the app! ... 
+3
source

I think you are on the right track with the default settings, for example:

 -(BOOL)isInitialRun { NSNumber *bRun = [[NSUserDefaults standardUserDefaults] valueForKey:@"initialRun"]; if (!bRun) { return YES; } return [bRun boolValue]; } -(void)setIsInitialRun:(BOOL)value { [[NSUserDefaults standardUserDefaults] setBool:value forKey:@"initialRun"]; } 

Then in your application deletion:

 if ([self isInitialRun]) { [self createPlist1]; [self createPlist2]; [self createPlist3]; [self setIsInitialRun:NO]; } 
+2
source

this is what i used:

 static dispatch_once_t once; dispatch_once(&once, ^ { // run once code goes here }); 
+2
source

As far as I know, the method you offer is the only option. Save the key in NSUserDefaults after the first start and check for the specified key.
However, you can also check each of your functions (createPlist1 - 3 functions) to check if PList exists. It would be a little cleaner.

+1
source

One thing I would add to @Seva Alekseyev:

After making any changes (ie [Def setObject:CurVer forKey:@"Version"]; ) you should call [Def synchronize]

I had a problem where changes made to NSUserDefaults using setObject were not saved until I used synchronization.

0
source

Source: https://habr.com/ru/post/1345756/


All Articles