简体   繁体   English

如何在发布更新中启用iCloud并迁移Core Data?

[英]How do I enable iCloud and migrate Core Data on a release update?

I have a released V1.0 of an iPhone App which uses Core Data but no iCloud. 我有一个发布的iPhone App V1.0,它使用Core Data但没有iCloud。 Now I will like to release V2.0 with iCloud support. 现在,我想发布具有iCloud支持的V2.0。

I am using https://github.com/mluisbrown/iCloudCoreDataStack/blob/master/iCloudCoreDataStack/PersistentStack.m as a template, and everything seems to work. 我使用https://github.com/mluisbrown/iCloudCoreDataStack/blob/master/iCloudCoreDataStack/PersistentStack.m作为模板,似乎一切正常。

Because the previous release did not have iCloud, and the new one will, I will like to migrate the local copy to the iCloud one on the first time the user opens the new version. 因为以前的版本没有iCloud,而新的版本具有iCloud,所以我想在用户第一次打开新版本时将本地副本迁移到iCloud。

How do I do that? 我怎么做? Can I simply somehow copy the whole sqlite file from local to iCloud? 我可以简单地将整个sqlite文件从本地复制到iCloud吗? The schema is the same between 1.0 and 2.0. 模式在1.0和2.0之间相同。

I plan to ask the user if they want to use local Vs. 我打算问用户是否要使用本地Vs。 iCloud (as per guidelines), but if they say Yes, I want to just do a bulk copy somehow. iCloud(按照准则),但是如果他们说是,我只想以某种方式进行批量复制。

Thanks 谢谢

You can't bulk copy, you have to use proper API [NSPersistentStoreCoordinator migratePersistentStore:...] and [NSPersistentStoreCoordinator removeUbiquitousContentAndPersistentStoreAtURL:...] with iCloud options to migrate the store to iCloud. 您无法批量复制,必须使用带有iCloud选项的正确API [NSPersistentStoreCoordinator migratePersistentStore:...][NSPersistentStoreCoordinator removeUbiquitousContentAndPersistentStoreAtURL:...]才能将商店迁移到iCloud。 I have attempted to document how to do this (but the example uses UIManagedDocument, although the Core Data APIs for migration to and from iCloud remain the same). 我试图记录如何执行此操作(但示例使用UIManagedDocument,尽管用于从iCloud迁移的核心数据API保持不变)。

Not only do you have to check the user preferences you also have to check whether they are logged in to iCloud. 您不仅需要检查用户首选项,还必须检查他们是否已登录到iCloud。 Also you have to migrate to iCloud and then you have to be able to migrate back to Local if the user changes the preference. 另外,您必须迁移到iCloud,然后如果用户更改首选项,则必须能够迁移回本地。 So you have to check whether the user has changed settings whenever the app becomes active. 因此,您必须在应用程序启用时检查用户是否更改了设置。 It is not that straight forward to do but it's not to hard once you figure out the logic required. 这并不是一件容易的事,但是一旦弄清所需的逻辑,这并不困难。

See the link below for a more detailed explanation, sample code, and a video showing the app working and the creation/removal of files in the local and iCloud containers: 请参阅下面的链接,以获取更详细的说明,示例代码和视频,以显示应用程序的工作以及本地和iCloud容器中文件的创建/删除:

http://ossh.com.au/design-and-technology/software-development/uimanageddocument-icloud-integration/ http://ossh.com.au/design-and-technology/software-development/uimanageddocument-icloud-integration/

Remember you will have to remove the UIManagedDocument specific code from the example. 请记住,您必须从示例中删除特定于UIManagedDocument代码。

I have just posted a sample iOS library style Core Data app which includes iCloud integration. 我刚刚发布了一个示例iOS库样式的Core Data应用程序,其中包括iCloud集成。 The app includes a Settings Bundle for the user to toggle "Use iCloud" preference settings and will migrate the store to and from iCloud depending on the users settings. 该应用程序包括一个设置捆绑包,供用户切换“使用iCloud”首选项设置,并将根据用户设置将商店与iCloud迁移。

Download from the link below - sorry about the documentation - will get around to that at some point but it works much the same way as the UIManagedDocument example. 从下面的链接下载-抱歉文档-在某个时候可以解决该问题,但其工作方式与UIManagedDocument示例大致相同。

http://ossh.com.au/design-and-technology/software-development/ http://ossh.com.au/design-and-technology/software-development/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM