Balakrishnan Mca
Balakrishnan Mca

Reputation: 747

core data migration xcode 4.2 is not update the data for app already available in app store

we are using core data for our online catalog and its working fine and app available in app store now I need to upgrade the core date with some of fields and attributes. It migrated to new one but old data already stored in the app are completely vanished. I tried various ways to retain it using this code

    NSString *databaseFilePath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"App_iOS.sqlite"];

    NSFileManager *fileManager = [NSFileManager defaultManager];
    [fileManager removeItemAtPath:databaseFilePath error:NULL];

    NSURL *storeUrl = [NSURL fileURLWithPath: databaseFilePath];

    NSError *error = nil;
    if( ![[self persistentStoreCoordinator] addPersistentStoreWithType:NSSQLiteStoreType  configuration: nil URL:storeUrl options:nil error:&error])
    {
        [__managedObjectContext insertObjects];
    }
    else
    {
        [__managedObjectContext updatedObjects];
    } 

I am not get solution yet to retain the data in app. I searched in internet for this most of the face the same problem but I am not receive the good solution yet

Upvotes: 1

Views: 709

Answers (2)

Balakrishnan Mca
Balakrishnan Mca

Reputation: 747

I've now found out that this is quite simple - once you know where to look.# In my AppDelegate I set-up the NSPersistentStoreCoordinator - and you need to add some options to this to tell it to handle auto-migrate:

    NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:

    [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,

    [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];

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

    if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType        configuration:nil URL:storeUrl options:options error:&error]) {
          // Handle error
        NSLog(@"Problem with PersistentStoreCoordinator: %@",error);
    }

Then you need to do in xCode:

1.  Select your xcdatamodeld file
2.  Select the Editor Menu at the top - then choose Add Model Version
3.  Now your xcdatamodeld file have two (modelname.xcdatamodel & modelname2.xcdatamodel ) .
4.  Now modelname.xcdatamodel  have the green check mark implies it is current version, but we need to change the modelname2.xcdatamodel as a current version
5.  Select the xcdatamodeld file and then select the View Menu at the top - then Choose Utilities - then Choose the Show File Inspector is shown in right side of Xcode and then Select the Versioned Core Data Model - have Current(DropDownList) - select modelname2(the one you just made current version have green check mark).
6.  Now when you install this version onto a device that has the old model - it will automatically upgrade that model to the new model.

This seems great and as simple as I wanted - but I think you need to be careful during development as you change a model - otherwise you will have to create a new version for each change.

I think what I will do is that I will keep all of the changed files and then once I get ready to deploy my update I'll delete all the in-between files and just deploy with the oldest and latest models.

Upvotes: 1

user523234
user523234

Reputation: 14834

You need to use data migration technique. I faced the similiar task a while back.
There are tutorial out there on this. Data migration.

Upvotes: 0

Related Questions