2012-06-13 78 views
1

在将我的应用程序从v1升级到v2时,我对Core Data Model进行了一些小改动。这些更改只是为模型添加新属性。轻量级迁移后如何从核心数据中删除数据

我已经版本与之前和之后的变化数据模型和我的应用程序委托执行以下代码:

- (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与另外的迁移选项。这段代码效果很好,我的数据库成功更新。我遇到的问题是数据库更新后,我需要刷新数据库中的所有数据,以便填充新列。我在想,我会从相关实体/表中删除数据,并强制应用程序重新下载具有添加的列/属性的新数据集。

我不知道如何/在哪里执行删除/更新。一般的应用流程是这样的:在对一个网络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:

    根据下面的反馈,我已经做了以下修改:

    改变了migrationNeeded从本地对AppDelegate中一个公共类变量。 在登录查看,我已经添加了以下方法:

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

    确实似乎对不对?代码工作并在迁移后删除数据并插入新副本。我只是讨厌在每次应用程序启动时检查迁移。

  • 回答

    0

    如果您知道如何确定何时删除旧数据,您只需要获取所需的所有入口并将其删除。这里是你如何做到这一点(例如,如果你想删除所有 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 
    
    +0

    感谢尼基塔,是指出我朝着正确的方向发展。我已经通过问题修改了可能的解决方案。 – Joshua

    +0

    对我来说似乎是对的 –

    +0

    感谢您的帮助! – Joshua

    相关问题