最近,我的一个生产应用程序遇到了一个问题,该应用程序使用核心数据
和 CloudKit
,其中数据在设备之间没有同步,经过一番研究,我发现需要初始化的私人CloudKit容器中的模式;我从来没有做过。
我仍然不是100%肯定的部分是何时运行 initaizecloudkitschema
方法,该方法已将应用发布到AppStore之后。我看到Apple建议在使用 #if debug
测试时运行它,但是...您每次在Xcode中编译时真的想运行它吗?
这是我在这一点上理解的方式...
- 应用程序版本,调用
initizecloudkitschema()
以匹配 core> core data
和 cloud> cloudkit
之间的模式。
- 添加或删除和属性,调用
initizecloudkitschema()
以更新 Cloudkit
schema。
- 重命名为属性,调用
initizecloudkitschema()
以更新 CloudKit
schema。
等等。
如果我上面的假设是正确的,请在开发过程中调用 prinitizecloudkitschema()
方法,将在 cloudkit
中更新架构,然后在AppStore中发布新应用程序,因此创建对于使用该应用程序的先前版本的现有用户的问题,因为它们将没有最新的代码,但是将使用包含新属性的最新架构。
在将应用程序发布到AppStore之后,有人可以在 CloudKit
中分享其处理模式更新的方法吗?
代码:
do {
try container.initializeCloudKitSchema()
} catch {
print(error)
}
I recently had an issue with one of my production apps that use Core Data
and CloudKit
where data wasn't syncing between devices, after a little bit of research I found out that the schema in the private CloudKit container needed to be initialized; which I never did.
The part I'm still not 100% sure is when to run the initializeCloudKitSchema
method after the app has been released to the AppStore. I see that Apple recommends running it when testing by using #if DEBUG
, but... do you really want to run it every time you compile in Xcode?
Here is how I understand it at this point...
- App release, call
initializeCloudKitSchema()
to match schemas between Core Data
and CloudKit
.
- Added or deleted and attribute, call
initializeCloudKitSchema()
to update the CloudKit
schema.
- Renamed an attribute, call
initializeCloudKitSchema()
to update the CloudKit
schema.
Etc.
If my assumption above is correct, calling the initializeCloudKitSchema()
method during development would update the schema in CloudKit
before the new app version is released in the AppStore, therefore creating an issue for existing users with previous versions of the app since they will not have the latest code but will be using the latest schema which contains the new attributes.
Can someone please share their method of handling schema updates in CloudKit
after the app has been released to the AppStore?
Code:
do {
try container.initializeCloudKitSchema()
} catch {
print(error)
}
发布评论
评论(1)
就我而言,我什至不需要运行
inationizecloudkitschema()
方法。这是我所做的对我有用的。我用两个设备在本地测试,并确保一切都按预期进行同步。当然,这是使用测试登录帐户在沙盒环境中在XCode中完成的。
然后我去了开发
CloudKit
容器,然后单击部署模式更改
。最后,我使用生产/常规用户帐户直接在两个不同设备上的App Store下载了该应用程序,然后对其进行了测试。一切都按预期工作。
完成
理论上讲,一旦您对架构感到满意后,您似乎需要将架构部署到生产CloudKit容器,并通过执行上述或可能调用
我没有尝试的initializecloudkitschema()。
旁注:一旦将模式部署到生产CloudKit容器中,您将不再删除或重命名实体或属性。另外,每次更新核心数据架构时,您都必须执行以上操作。请记住,如果您在核心数据中添加或删除实体或属性,则必须创建一个新版本的核心数据容器以执行所谓的光迁移。
编辑:这是如何创建核心数据容器的新版本。
In my case, I didn't even need to run the
initializeCloudKitSchema()
method. Here is what I did that worked for me.I tested locally with two devices and made sure everything was syncing as expected. This of course was done in Xcode within the sandbox environment using testing login accounts.
Then I went to the development
CloudKit
container and clicked on theDeploy Schema Changes
.Lastly I went and downloaded the app directly from the App Store on two different devices using a production/regular user account and tested it. Everything worked as expected.
Done
In theory, it looks like you need to deploy the schema to the Production CloudKit container once you're satisfied with the schema and the results in the testing environments by doing the above or possibly calling the
initializeCloudKitSchema()
which I didn't try.Side notes: It looks like once you deploy your schema to the Production CloudKit container you no longer can delete or rename Entities or Attributes. Also, you will have to do the above every time you update the Core Data schema. Keep in mind that if you add or remove Entities or Attributes in Core Data, you must create a new version of the Core Data container to do what is called a light migration.
EDIT: Here is how to create a new version of the Core Data container.