本地帐户
从关于核心数据和iCloud的WWDC 2013 207 session中:
You provide us a single store URL inside the application's local sandbox and we then create an opaque container with an entry inside of it for each account on the system, including the local account, which is our term for what happens when there is no iCloud account on the system. This is a special store that's managed by Core Data so that you don't have to do anything special because your user doesn't have an iCloud account.
在iOS 7/OS X 10.9中,带有iCloud的Core Data将在关闭iCloud的情况下自动使用本地帐户。与后备存储(在iCloud启用但无法访问时使用)不同,当服务开启时,本地帐户将完全由iCloud帐户替换,而不会进行任何合并。仅当iCloud关闭时,本地帐户中的数据才可访问。在以下情况下会发生这种情况:
以上是我从实验中了解的内容。如果我错了,请纠正我。
数据消失时
按原样使用,本地帐户用户体验很糟糕。如果您在关闭iCloud的情况下向应用程序添加数据然后再将其打开,则数据将“消失”,并且您可能会认为它已被删除。如果您在启用iCloud的情况下向应用程序添加数据,然后将其关闭,则数据也会“消失”。
我已经看到了一些示例,这些示例试图通过在应用程序中添加(更多)iCloud设置并管理自己的“本地”存储(不是iCloud提供的存储)来解决此问题。重复工作给我的感觉到了。
利用本地帐户进行数据迁移
这种方法怎么样?
这类似于“提醒”所做的事情。但是,提醒会直接从iCloud设置询问用户有关数据迁移的信息,这是我们开发人员无法做到的。
问题
1)这种方法是否具有乍看之下并不明显的任何缺陷或边界情况?也许我们不是要像这样使用iCloud生成的本地帐户。
2)
NSPersistentStoreCoordinatorStoresWillChangeNotification
和NSPersistentStoreCoordinatorStoresDidChangeNotification
是否足以检测所有可能的iCloud转换?3)您会在
NSPersistentStoreCoordinatorStoresWillChangeNotification
和NSPersistentStoreCoordinatorStoresDidChangeNotification
之间进行用户提示和合并,还是收集其中的所有信息并等到商店更改为止?我问是因为这些通知似乎是在后台发送的,阻止它们执行可能较长的操作可能不是Core Data期望的。
最佳答案
我认为您误解了207届 session 上的讲话。
Core Data不会自动为您创建本地存储和iCloud存储,也不会自动关闭iCloud帐户时会同步数据的存储。根据用户选择的内容,您必须使用NSPersistentStoreUbiquityNameKey选项(对于iCloud存储)还是不使用(对于本地存储)来创建存储。
因为在初次安装应用程序时,新应用程序“数据和文档”的默认安全设置为“开”,所以您必须询问用户是否要使用iCloud。尝试使用Apple的Pages应用程序。
如果用户随后更改了首选项设置,则您的应用必须将商店迁移到iCloud或从iCloud迁移。
Core Data自动处理的部分是,如果您切换iCloud帐户(注销并使用其他帐户登录),则该应用程序将以登录该帐户时可能创建的Core Data存储运行。
请参阅下面的文字记录,其中清楚地说明该帐户消失后,iCloud存储将被删除。没了,卡普特,一只死了的鹦鹉。因此,尽管您有机会仅将更改日志保存在本地,以防将来再次使用该帐户。
You simply implement your will change handlers and respond to NSPersistentStoreCoordinator Stores Will Change and will notify you automatically when we need to change the persistent store file because there's new account on the system.
Of course, you can then call NSManagedObjectContext save and NSManagedObjectContext reset.
Now once you've done that, we'll remove the store from the coordinator just as with the asynchronous setup process and then we'll send you NSPersistentStoreCoordinator Storage Did Change notification, again, just like asynchronous setup and you can begin working with your application as you normally would.
Now, let's talk about this in a little bit more detail.
When you receive NSPersistentStoreCoordinator Stores Will Change notification, the persistent store is still available to use, and so unlike what we advised you of last year where you had to immediately drop the persistent store and wipe out your managed object context, you can still write to the managed object context and those changes will be persistent locally to be imported to the account if it every comes back.
This means that although your user's changes won't make it to iCloud immediately, if they ever sign in again, they'll be there and waiting.
Finally, all of these store files will be managed by Core Data and that means that we could remove them at any time.
Each store will be removed once its account has gone away because we can rebuild the file from the cloud.
So we want to free up as much disk space as possible for your application to use and not have old store files lying around that could take up additional resources.
并进一步
We're also introducing a new option to help you create backups or local copies of the iCloud persistent store called NSPersistentStore Remove Ubiquitous Metadata Option.
This removes all associated metadata from the iCloud store; that means, anything that we write into the metadata dictionary as well as the store file itself, and it's critical if you want to use the migration API to create backups or local copies at a persistent store you wish to open without the iCloud options.
还可以查看蒂姆·罗德利(Tim Roadley)的书与勘误的链接
http://timroadley.com/2014/02/13/learning-core-data-for-ios-errata/
如果您登录到iCloud,然后用户更改了应用程序首选项设置(与“数据和文档”安全性设置不同)以关闭iCloud,则应询问用户是否要将现有的iCloud存储区迁移到本地(再次-使用Pages尝试一下,看看收到什么消息。
我已经发布了一个示例应用程序,可在此处完成所有这些操作。观看视频以查看预期的行为。
http://ossh.com.au/design-and-technology/software-development/
示例应用程序的一些功能包括:
功能包括:
关于ios - 开启/关闭iCloud时迁移数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21895343/