核心数据迁移xcode 4.2不会更新应用商店中已有的应用数据

时间:2011-12-27 06:26:32

标签: iphone objective-c ios xcode cocoa-touch

我们正在使用我们的在线目录的核心数据及其在app store中可用的工作正常和应用程序现在我需要使用一些字段和属性来升级核心日期。它迁移到新的,但已经存储在应用程序中的旧数据完全消失了。我尝试了各种方法来使用此代码保留它

    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];
    } 

我还没有得到解决方案来保留app中的数据。我在互联网上搜索这个大部分面临同样的问题,但我还没有收到好的解决方案

1 个答案:

答案 0 :(得分:1)

我现在发现这很简单 - 一旦你知道在哪里看。# 在我的AppDelegate中,我设置了NSPersistentStoreCoordinator - 您需要为此添加一些选项以告诉它处理自动迁移:

    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);
    }

然后你需要在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.

这看起来很棒,也很简单 - 但我认为在更改模型时你需要在开发过程中小心 - 否则你将不得不为每次更改创建一个新版本。

我认为我将要做的是保留所有已更改的文件,然后在我准备好部署更新后,我将删除所有中间文件,并使用最旧和最新的模型进行部署。 / p>