代码之家  ›  专栏  ›  技术社区  ›  Massimo Cafaro

核心数据迁移失败,错误为:第一次迁移后未能保存新存储

  •  4
  • Massimo Cafaro  · 技术社区  · 14 年前

    reason=“第一次迁移后保存新存储失败。”; }

    我尝试过使用 NSMigratePersistentStoresAutomaticallyOption NSInferMappingModelAutomaticallyOption 以及仅使用 NSMigratePersistentStoresAutomatically选项 ,提供了从v2到v3的映射模型。

    我看到上面的错误被记录,并且应用程序中没有可用的对象。但是,如果我退出应用程序并重新打开它,一切都会就绪并正常工作。

    我使用的核心数据方法如下

    - (NSManagedObjectModel *)managedObjectModel {
    
        if (managedObjectModel != nil) {
            return managedObjectModel;
        }
    
    
    
        NSString *path = [[NSBundle mainBundle] pathForResource:@"MYAPP" ofType:@"momd"];
        NSURL *momURL = [NSURL fileURLWithPath:path];
        managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:momURL];
    
        return managedObjectModel;
    
    }
    - (NSManagedObjectContext *) managedObjectContext {
    
        if (managedObjectContext != nil) {
            return managedObjectContext;
        }
    
    
        NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
        if (coordinator != nil) {
            managedObjectContext = [[NSManagedObjectContext alloc] init];
            [managedObjectContext setPersistentStoreCoordinator: coordinator];
        }
        return managedObjectContext;
    }
    
    
    
    - (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
    
        if (persistentStoreCoordinator != nil) {
            return persistentStoreCoordinator;
        }
    
    
        NSURL *storeUrl = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"MYAPP.sqlite"]];
    
    
      NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
      [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
      [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];
    
      NSError *error = nil;
      persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: [self managedObjectModel]];
       if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:options error:&error]) {
            // Handle error
            NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
       }  
    
    
    return persistentStoreCoordinator;
    
    }
    

    BOOL oldExists = [[NSFileManager defaultManager] fileExistsAtPath: [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"MYAPP~.sqlite"]];
    

    总是不回答。有什么线索吗?我做错什么了吗?

    2 回复  |  直到 14 年前
        1
  •  1
  •   bmasters    14 年前

    我也遇到了这个问题,在阅读了尽可能多的苹果文档和网络帖子后,我发现似乎没有答案。在我的例子中,手动迁移也很有效,但是当我打开一个新的协调器时,它会给出与您相同的错误。最后,我决定回到上一个工作版本的数据模型,做一系列小的更改/版本,看看它在哪里破坏了自动迁移功能,进一步深入研究,结果发现它没有。现在我可以添加实体、属性和关系而不出问题,它们会自动迁移。有没有可能删除了数据模型的临时版本?

        2
  •  1
  •   c roald    11 年前

    值得一提的是,Magical Record Core Data实用程序包包含以下黑客攻击:

    [coordinator MR_addAutoMigratingSqliteStoreNamed:storeFileName];
    
    //HACK: lame solution to fix automigration error "Migration failed after first pass"
    if ([[coordinator persistentStores] count] == 0) 
    {
        [coordinator performSelector:@selector(MR_addAutoMigratingSqliteStoreNamed:) withObject:storeFileName afterDelay:0.5];
    }
    

    你可以试试类似的。不过,我一直找不到问题所在的解释,也找不到为什么重试会奏效的原因。

    推荐文章