Core data 如何在轻量级迁移后从核心数据中删除数据

Core data 如何在轻量级迁移后从核心数据中删除数据,core-data,ios5,Core Data,Ios5,在将我的应用程序从v1升级到v2的过程中,我对核心数据模型做了一些小改动。更改只是向模型添加新属性 我已使用前后更改对数据模型进行了版本控制,并在我的应用程序委派中实现了以下代码: - (NSPersistentStoreCoordinator *)persistentStoreCoordinator { if (__persistentStoreCoordinator != nil) { return __persistentStoreCoordinator;

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

我已使用前后更改对数据模型进行了版本控制,并在我的应用程序委派中实现了以下代码:

- (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上的MigrationRequired从本地类变量更改为公共类变量。 在登录视图中,我添加了以下方法:

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

这看起来对吗?代码正常工作,数据在迁移后被删除,并插入新的副本。我讨厌每次应用程序启动时都检查迁移情况。

如果你知道如何确定何时删除旧数据,那么你所需要的就是获取所有需要的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

谢谢尼基塔,这为我指明了正确的方向。我提出了一个可能的解决办法。