如何解决重命名关系时的核心数据和CloudKit集成问题代码134110
我目前在App Store中有一个使用Core Data的应用程序:该应用程序使人们可以记录其水上运动和帆船运动(想像水手的Strava)。我有3年没有更新过该应用程序了,该应用程序似乎在最新的iOS版本上仍然可以正常运行,但是我最近计划对其进行改进。
我目前正在为该应用程序进行更新,需要更改数据模型和架构。我想要一个自动的轻量级迁移。我重命名了一些实体,属性和关系,但确保将先前的ID放在编辑器的“重命名ID”字段中。 我想借此机会在CloudKit上同步更新的架构。我按照Apple Developer文档上的说明设置了同步。我还确保使用initializeCloudKitSchema()初始化架构。当我访问仪表板时,我看到正确的架构。容器仅处于开发模式,不能投入生产。
当我使用可用应用程序生成的sqlite文件启动应用程序时,由于数据仍在此处且正确无误,因此迁移工作似乎很好。我可以正常浏览该应用程序,当我访问CloudKit仪表板时,数据已正确保存。
但是有时,应用程序在启动时崩溃,并显示以下错误:
UserInfo={reason=CloudKit integration forbids renaming 'crewMembers' to 'sailors'.
Older devices can't process the new relationships.
NSUnderlyingException=CloudKit integration forbids renaming 'crewMembers' to 'sailors'.
Older devices can't process the new relationships.}}}
Fatal error: Unresolved error Error Domain=NSCocoaErrorDomain Code=134110 "An error occurred during persistent store migration."
有关的实体已重命名,因为这些关系以及该关系在双方都是多对多的,可选的。即使我重置了CloudKit开发容器,也会发生这种情况。我真的不清楚何时出现(似乎是随机的,在我更新了一些数据之后或在我更新了核心数据模型之后)。知道为什么应用程序崩溃了吗?我想尽可能地为我的实体和关系保留新的命名。
- SKPRCrewMemberMO重命名为Sailor
- SKPRTrackMO重命名为Activity
- crewMembers >跟踪已重命名的水手>活动
以下是迁移问题起源时该实体的先前数据模型和更新数据模型的一些屏幕截图,以及一些有关我的Core Data Stack初始化和控制台错误的代码。
PS:该应用程序被数百人使用。数量虽然不多,但仍然有一些活动记录在案,我不想破坏任何东西或丢失或破坏数据。我可以启动一个新应用程序,但用户只会在本地保存在共享容器中(我曾想与Apple Watch扩展程序共享Core Data时使用了应用程序组),因此用户会失去进度。而且我会失去用户群和与App Store相关的东西。
private init() {
container = NSPersistentCloudKitContainer(name: "Skipper")
guard let description = container.persistentStoreDescriptions.first else {
fatalError("###\(#function): Failed to retrieve a persistent store description.")
}
description.setOption(true as NSNumber,forKey: NSPersistentHistoryTrackingKey)
description.setOption(true as NSNumber,forKey: NSPersistentStoreRemoteChangeNotificationPostOptionKey)
let id = "iCloud.com.alepennec.sandbox20201013"
let options = NSPersistentCloudKitContainerOptions(containerIdentifier: id)
description.cloudKitContainerOptions = options
container.loadPersistentStores(completionHandler: { (storeDescription,error) in
if let error = error as NSError? {
fatalError("Unresolved error \(error),\(error.userInfo)")
}
})
do {
try container.initializeCloudKitSchema()
} catch {
print("Unable to initialize CloudKit schema: \(error.localizedDescription)")
}
container.viewContext.automaticallyMergesChangesFromParent = true
container.viewContext.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。