【问题标题】:Core Data - can't achieve a simple LightWeight Migration核心数据——无法实现简单的轻量级迁移
【发布时间】:2016-12-12 15:04:20
【问题描述】:

我无法通过简单地将 1 个实体添加到数据模型来实现简单的轻量级迁移。

我已阅读并遵循所有指南/文档/帖子/答案,我似乎找不到我的错误/错误。

  • 我确实从现有的数据模型创建了一个新的数据模型。
  • 我确实已将新数据模型设置为当前数据模型。
  • 我确实只向新数据模型添加了 1 个实体(+ 到父实体的链接)。
  • 我确实在方法addPersistentStoreWithType 中传递了字典选项NSMigratePersistentStoresAutomaticallyOptionNSInferMappingModelAutomaticallyOption

我什至尝试记录所有内容,感谢这篇文章提供的方法:core data migration

/*! The method checks the Core Data file version is compatible with the App's model version
 and then pushes the main menu view onto the navigation stack.  If not compatible it displays a
 message to the user.
 
 @param file The file URL for the Core Data Store. With UIManagedDocument you have to get the
 actual store file URL, you can't just use the UIManagedDocument file URL.
 */
-(void) checkCoreDataFileVersion:(NSURL*)file
{
    if ([self checkVersion:file]) {
        
    // file version is compatible so continue (add code to push the menu view)
        
    } else {
        
        // file version is NOT compatible
        
        _fileOpenErrorAlert = [[UIAlertView alloc] initWithTitle:@"Unable to open Document" message:@"Please check that you have the correct application version installed" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [_fileOpenErrorAlert show];
        
    }
    return;
}


/*! Checks the Core Data files models version against the apps model version to see if they
 are compatible.  This will return YES if a lightweight migration can be performed and NO if NOT.
 
 @param fileURL The file URL for the Core Data Store. With UIManagedDocument you have to get the
 actual store file URL, you can't just use the UIManagedDocument file URL.
 @return  Returns YES if they are compatible and NO if not.
 */
- (bool)checkVersion:(NSURL*)fileURL {
    
    NSManagedObjectModel *model = [self managedObjectModel];
      
    NSLog(@" app model entity version hashes are %@", [model entityVersionHashesByName]);
      
    NSError *error;
    NSDictionary *metaData = [NSPersistentStoreCoordinator metadataForPersistentStoreOfType:NSSQLiteStoreType URL:fileURL error:&error];
      
    if (!metaData) {
        NSLog(@"problem getting metaData");
        NSLog(@"  - error is %@, %@", error, error.userInfo);
        return NO;
    }
                      
    bool result = [model isConfiguration:nil compatibleWithStoreMetadata:metaData];
    if (!result) {
        NSLog(@" file is not compatible!");
        NSLog(@" metadata is %@", metaData);
    }
                                      
    return result;
                                      
}

当我对所有实体的元数据进行差异时,我只匹配 1 个实体(新创建的)的差异。那么为什么它不能进行迁移呢?我刚刚添加了 1 个实体。

编辑:

我没有崩溃,应用程序运行良好。 有一点我不明白。当我从 AppStore 下载我们最新的应用程序并启动它时,当我从 xCode 构建我最新的开发应用程序(使用新数据模型)而不是来自 AppStore 的应用程序时,不会发生迁移。

但是当我使用 GIT 时,当我将 HEAD 放到最新版本标签时,构建,启动应用程序。然后将 HEAD 放回我最新的开发功能(使用新的数据模型等),构建并运行,迁移完成,一切正常。

那么我应该相信哪种情况呢?

【问题讨论】:

  • 请添加错误描述/崩溃日志
  • 我已经编辑了我的问题

标签: ios core-data database-migration


【解决方案1】:

是的,您应该信任第二个 senario,通过将其应用于最后发布的代码来测试 coredata 迁移。 第一个场景不再有效,因为出于某些安全原因,Apple 不再提供直接使用 xcode 更新通过 itune 下载的应用程序的能力。

有一种方法可以在 itune-version 上测试升级,但不能直接从 xcode 进行。

Technical Note TN2285 Testing iOS App Updates

安装更新的存档版本的临时分发 在已安装旧版应用程序的设备上使用 iTunes 已安装。

Installing Your App on Test Devices Using iTunes

【讨论】:

  • 那么,如果我已经在手机上安装了 AppStore 中的应用程序,为什么当我使用 TestFlight 时迁移不起作用?这不是更新的真实案例吗?它仅适用于 GIT
  • 不一样,如果您安装了 itune/appStore 版本,要测试更新,您需要使用 itunes 安装新版本...查看技术说明。
  • 使用 iTunes 在测试设备上安装您的应用程序检查:developer.apple.com/library/content/documentation/IDEs/…
  • 谢谢,我正在尝试,如果可行,我会接受你的回答(我想会的)。
  • 很高兴为您提供帮助:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-03-02
  • 2011-06-12
  • 1970-01-01
  • 1970-01-01
  • 2014-05-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多