Radu
Radu

Reputation: 3494

Clean core data when updating app to App Store

I have been using lightweight migration for updating data model. I want to make a release where I clean the data base and do not migrate the data. What is the best way to do that? Has anybody did this whit a App Store release?

UPDATE

NSDictionary *options = @{NSMigratePersistentStoresAutomaticallyOption: @NO,
                         NSInferMappingModelAutomaticallyOption: @NO};

NSError *error = nil;
__persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];

if (![__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error]) {

    if (![[NSUserDefaults standardUserDefaults] valueForKey:kCleanDB]) {
        NSLog(@"Deleting sql database, due to unresolved error %@, %@", error, [error userInfo]);

        [[NSFileManager defaultManager] removeItemAtPath:storeURL.path error:&error];
        [[NSUserDefaults standardUserDefaults] setValue:[NSNumber numberWithBool:YES] forKey:kCleanDB];
    }

    if (![__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error]) {
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }

Upvotes: 1

Views: 266

Answers (1)

Ashley Mills
Ashley Mills

Reputation: 53111

All you need to do is delete your persistent store on startup, before you create a managed object context.

If you're using Apple's standard pattern to create the Core Data stack, when you access the MOC it'll create a new, empty store.

You'll need to track whether you've done this "upgrade". To do that just store a flag in NSUserDefaults once it's done, and check each time to make sure you only do it once.

Upvotes: 2

Related Questions