1

在将我的应用程序从 v1 升级到 v2 时,我对核心数据模型进行了一些小改动。这些更改只是为模型添加了新属性。

我已经使用前后更改对数据模型进行了版本化,并在我的 App Delegate 中实现了以下代码:

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
    if (__persistentStoreCoordinator != nil)
    {
        return __persistentStoreCoordinator;
    }

    NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"ISDEmployees.sqlite"];

    NSLog(@"storeURL:%@",storeURL);

    NSError *error = nil;

    // Create a dictionary for automatic lightweight core data migration
    NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
                             [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
                             [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, 
                             nil];

    __persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];

    // Set up the persistent store and migrate if needed
    if (![__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error])
    {
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }

    return __persistentStoreCoordinator;
}

基本上是标准的 persistentStoreCoordinator 加上迁移选项。此代码运行良好,我的数据库已成功更新。我遇到的问题是,在数据库更新后,我需要刷新数据库中的所有数据,以便填充新列。我在想我会从相关实体/表中删除数据并强制应用程序重新下载带有添加的列/属性的新数据集。

我不确定如何/在哪里执行删除/更新。一般申请流程是这样的:

  • 使用 Web API 验证登录
  • 成功登录后,调用 API 并获取最新添加/更新的记录。
  • 显示更新的数据

我知道我可以通过将此代码添加到 persistentStoreCoordinator 来检查是否需要迁移:

// Get the current data store meta data
BOOL migrationNeeded = NO;
NSDictionary *existingStoreData = [NSPersistentStoreCoordinator metadataForPersistentStoreOfType:NSSQLiteStoreType URL:storeURL error:&error];

if (existingStoreData) 
{
    // Check to see if new model is not the same as the existing mode, meaning a migration is required
    if (![self.managedObjectModel isConfiguration:nil compatibleWithStoreMetadata:existingStoreData]) 
    {
        migrationNeeded = YES;
    }
}

任何帮助将不胜感激!!

更新#1:

根据以下反馈,我进行了以下更改:

在 AppDelegate 上将 migrationNeeded 从本地更改为公共类变量。在登录视图中,我添加了以下方法:

- (void)checkForDatabaseMigration
{
    // Get a copy of the managed object context. If a migration is needed, it will kick it off
    NSManagedObjectContext *managedObjectContext = [(AppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];

    if ([(AppDelegate *)[[UIApplication sharedApplication] delegate] migrationNeeded]) 
    {
        // Delete all data from the table
    }

    managedObjectContext = nil;
}

这看起来对吗?代码有效,迁移后数据被删除并插入新副本。我只是讨厌每次应用程序启动时检查迁移。

4

1 回答 1

0

如果您知道如何确定何时删除旧数据,那么您只需获取所需的所有实体并删除它们即可。这是你如何做到的(例如,如果你想删除所有Man enteties):

    NSFetchRequest * request = [[NSFetchRequest alloc] init];
[request setEntity:[NSEntityDescription entityForName:@"Man" inManagedObjectContext:myContext]];
[request setIncludesPropertyValues:NO]; //only fetch the managedObjectID

NSError * error = nil;
NSArray * men = [myContext executeFetchRequest:request error:&error];
//error handling goes here
for (NSManagedObject * man in men) {
  [myContext deleteObject:man];
}
NSError *saveError = nil;
[myContext save:&saveError];
//more error handling here
于 2012-06-13T19:30:57.357 回答