Key data: after migration, additional migration code

I want to migrate from my data model version1 to version2, but after the migration is complete I want to execute some custom migration code. How to find out when migration occurs? Is there a migHassCompleed delegate method or notification?

For interests: the custom migration code I want to execute resizes the png in the database.

+3
source share
2 answers

Well, you can run this code immediately after the migration, in setting up a permanent repository configuration:

+ (NSPersistentStoreCoordinator *)persistentStoreCoordinatorForStore:(NSString *)store {
    NSURL *storeUrl = [NSURL fileURLWithPath:[[[self class] applicationDocumentsDirectory] stringByAppendingPathComponent:store]];

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

    // Check for model changes without trying to update
    if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:nil error:&error]) {
        // Set the automatic update options for the current model
        NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
                                 [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
                                 [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption,
                                 nil];

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

            NSLog(@"Error opening the database. Deleting the file and trying again.");

            //delete the sqlite file and try again
            [[NSFileManager defaultManager] removeItemAtPath:storeUrl.path error:nil];
            if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:options error:&error])
            {
                /*
                 Replace this implementation with code to handle the error appropriately.

                 abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button.

                 Typical reasons for an error here include:
                 * The persistent store is not accessible
                 * The schema for the persistent store is incompatible with current managed object model
                 Check the error message to determine what the actual problem was.
                 */
                NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
                abort();
            }
        } else { // The model was updates successfuly
            // Implement here your custom migration code
        }

    }    

    return [persistentStoreCoordinator autorelease];
}

Greetings

Vfn

0
source

, , , , , .

NSError *error;
NSDictionary *sourceMetadata = [NSPersistentStoreCoordinator metadataForPersistentStoreOfType:NSSQLiteStoreType
                                                                                          URL:storeURL
                                                                                        error:&error];
NSManagedObjectModel *destinationModel = [persistentStoreCoordinator managedObjectModel];
BOOL migrationRequired = ![destinationModel isConfiguration:nil compatibleWithStoreMetadata:sourceMetadata];

// Now add persistent store with auto migration, and do the custom processing after
+2

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


All Articles