Question

i have few line code which only called once at first time of app launch in didFinishLaunchingWithOptions method,but now the problem is i need to called that part of code in new version of app because i made some changes there.as we know defaults are not deleted on updation so which way will be better to fix it.here is my code.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
if(![fn chkAppRunningFirstTime])
    {
        [fn createFolder:@"productImages"];
        [fn addSkipBackupAttributeToItemAtURL:@"productImages"];
        [fn createFolder:@"productImages/thumb"];
        [fn createFolder:@"productImages/medium"];
        [fn createFolder:@"productImages/large"];

        [db executeQuery:"drop table productImgTbl;"];
        [db executeQuery:"create table productImgTbl ('no' INTEGER PRIMARY KEY AUTOINCREMENT, 'img_name' text, 'img_url' text);"];
}
return YES;
}

fn is my Function Class where i am saving value like this way.

- (BOOL)chkAppRunningFirstTime
{
    return [defaults boolForKey:@"firstTime"];
}

- (void)setFirstTime:(BOOL)val
{
    [defaults setBool:val forKey:@"firstTime"]; 
    [defaults synchronize];
}

Any kind of suggestion will be appriacted. Thanks

Était-ce utile?

La solution

To me your logic is a little backwards.

- (BOOL)chkAppRunningFirstTime
{
    return [defaults boolForKey:@"firstTime"];
}

This method returns the logically opposite value to the method name. When it is the first time it returns NO...

- (void)setFirstTime:(BOOL)val
{
    [defaults setBool:val forKey:@"firstTime"]; 
    [defaults synchronize];
}

This method shouldn't take a parameter. It can only be the first time once so setting the flag always has the same value.

As the logic is backwards, you would be better to change the name of the key used, for example to initialisationHasExecuted.

The only other thing to consider is version number. Will you need to run other update code in future versions? How should that be handled in relation to this?

Autres conseils

For this, you can store the previous version number in your app. Technically, you save the current versionNumber of your app (you can get it from the info-plist) for the key previous version. Then you can check on first launch whether the previous version matches the current version. If it does not match, you know you are an new version. Update the previous Version key in Userdefaults with the current version, done.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top