Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Core Data Migration: How to delete the Core Data stack?

My plan is to delete the old Core Data stack (the NSManagedObjectModel .momd file & the NSPersistentStore .sqlite file) because:

  • I don't have experience with Core Data migrations.
  • the new .xcdatamodel schema is completely different than the old one.
  • I can safely delete the user's old data because it's all stored on our server and the new app downloads the latest data from our server anyway.

In this case, is complete deletion the best way to go about migration?

like image 443
ma11hew28 Avatar asked Jan 16 '12 20:01

ma11hew28


People also ask

How do I delete everything from my Core Data?

Delete Everything (Delete All Objects, Reset Core Data) One approach to delete everything and reset Core Data is to destroy the persistent store. Deleting and re-creating the persistent store will delete all objects in Core Data.

Where is Core Data saved?

The persistent store should be located in the AppData > Library > Application Support directory.

What is Core Data migration?

Core Data can typically perform an automatic data migration, referred to as lightweight migration. Lightweight migration infers the migration from the differences between the source and the destination managed object models.

How does Core Data save data?

Most interactions with Core Data will occur through an instance of NSManagedObjectContext : the portal through which our app will create new entities, save changes, and fetch from the store. The persistent container comes with a NSManagedObjectContext as one of its built-in properties.


2 Answers

It is a perfectly valid thing to do if your app requires internet access anyway. Otherwise users may be left with an empty data set (you delete the old database when you find it's incompatible with the current model, but you cannot re-populate it without access to the server).

Technically, that's a trivial thing to do. When you set up the NSPersistentStoreCoordinator:

NSURL *storeURL = ...;
NSManagedObjectModel *managedObjectModel = ...;
NSError *error = nil;
_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: managedObjectModel];

// Check if we already have a persistent store
if ( [[NSFileManager defaultManager] fileExistsAtPath: [storeURL path]] ) {
    NSDictionary *existingPersistentStoreMetadata = [NSPersistentStoreCoordinator metadataForPersistentStoreOfType: NSSQLiteStoreType URL: storeURL error: &error];
    if ( !existingPersistentStoreMetadata ) {
        // Something *really* bad has happened to the persistent store
        [NSException raise: NSInternalInconsistencyException format: @"Failed to read metadata for persistent store %@: %@", storeURL, error];
    }

    if ( ![managedObjectModel isConfiguration: nil compatibleWithStoreMetadata: existingPersistentStoreMetadata] ) {
        if ( ![[NSFileManager defaultManager] removeItemAtURL: storeURL error: &error] )
            NSLog(@"*** Could not delete persistent store, %@", error);
    } // else the existing persistent store is compatible with the current model - nice!
} // else no database file yet

[_persistentStoreCoordinator addPersistentStoreWithType: NSSQLiteStoreType 
                                          configuration: nil 
                                                    URL: storeURL 
                                                options: nil 
                                                  error: &error];
like image 79
Costique Avatar answered Oct 21 '22 08:10

Costique


If you create a blank Core Data application you find the necessary code in Apples comments in the Application Delegate:

If you encounter schema incompatibility errors during development, you can reduce their frequency by:

  • Simply deleting the existing store: [[NSFileManager defaultManager] removeItemAtURL:storeURL error:nil]

  • Performing automatic lightweight migration by passing the following dictionary as the options parameter:@{NSMigratePersistentStoresAutomaticallyOption:@YES, NSInferMappingModelAutomaticallyOption:@YES} Lightweight migration will only work for a limited set of schema changes; consult "Core Data Model Versioning and Data Migration Programming Guide" for details.

like image 42
MacMark Avatar answered Oct 21 '22 07:10

MacMark