Terry Bu Terry Bu - 21 days ago 8
iOS Question

Core Data ios - how to load data depending on app first launch or not?

I'm trying to make a Core Data persistence store logic where:


  1. If it's the first time the user is launching the app, all the data gets generated inside the app and gets loaded (and when he/she exists app, that's the first time the data is getting saved to the persistent store)

  2. If it's not the first time the user is launching the app, then all the data gets loaded from existing Core Data persistent store.



How would you check if it's the first time the user is launching the app?

Answer

You can store the information with NSUserDefaults. You set a variable to TRUE when the app launches for the first time.

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];

if([userDefaults boolForKey:@"notFirstLaunch"] == false)
{
    //do stuff on first launch.

    [userDefaults setBool:YES forKey:@"notFirstLaunch"];
    [userDefaults synchronize];
}

You can also store the app Version to update the database on App updates.

Comments