How to make iPhone application tolerant of CoreData schema changes

I have an application that makes extensive use of the CoreData API. I am developing an updated version that adds several fields to an object. My application crashes on loading (unless I donate private storage and start again) due to schema changes.

The problem is when clients upgrade to the new version, I would not miss the procedure for updating my data the first time I load the application, but I can’t even download it because CoreData doesn’t like that scheme change.

Is there a way to talk about CoreData "Its okay .. don't worry about changing the circuit"? Because I just added the fields and did not rename or delete anything.

+3
source share
1

, Marcus Zarra Core Data ( 5). , , , . -, (), . -, , " ". -, , NSPsistentStoreCoordinator , ( ) .

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
    {
    if (persistentStoreCoordinator)
        return persistentStoreCoordinator;

    NSURL *storeUrl = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"MyDataStore.sqlite"]];

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

    // Use mapping model
    NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
                             [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, nil];


    if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType 
                                configuration:nil
                                URL:storeUrl
                                options:options
                                error:&error])
        {
        [NSApp presentError:error];
        }
    return persistentStoreCoordinator;
    }

, . , , , , . , , , , / , .

+6

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


All Articles