2011-12-27 60 views
1

我们正在使用核心数据作为我们的在线目录,并且其工作正常且应用商店中可用的应用现在我需要升级核心日期,字段和属性。它已经迁移到新的,但已经存储在应用程序中的旧数据完全消失。我试过各种方法来保留它使用此代码核心数据迁移xcode 4.2不会更新应用商店中已有应用的数据

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

我还没有得到解决方案,但仍然保留在应用程序中的数据。我在互联网上搜索了这个大部分的面对同样的问题,但我还没有收到良好的解决方案,但尚未

+0

[link] http://stackoverflow.com/questions/1018155/what-do-i-have-to-do-to-get-core-data-to-automatically-migrate-models这个答案非常有帮助实现核心数据更新。感谢所有! – 2011-12-29 11:36:26

回答

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. 

这似乎是伟大的,因为我想那样简单 - 但我认为你需要为你改变发展过程中要小心模型 - 否则你会有t o为每个更改创建一个新版本。

我想我会做的是我会保留所有更改的文件,然后一旦准备好部署我的更新,我将删除所有中间文件,并部署最旧和最新的模型。

0

您需要使用数据迁移技术。我一会儿面临类似的任务。
有关于此的教程。 Data migration.