简体   繁体   English

iOS,Core Data和iCloud-切换上下文

[英]iOS, Core Data and iCloud - switching context

I am creating an app where data needs to be displayed right away from the local datastore. 我正在创建一个应用程序,该应用程序需要立即在本地数据存储中显示数据。 I fire off a background thread when the app starts to determine if iCloud is available. 当应用开始确定iCloud是否可用时,我触发了后台线程。

I looked everywhere and can't find the solution to this: when iCloud becomes available, can I change the "options" on the persistentStore to start using iCloud transactions? 我到处都是,找不到解决方案:当iCloud可用时,是否可以更改persistentStore上的“选项”以开始使用iCloud事务?

I'm not sure what the proper approach is in this situation. 我不确定在这种情况下哪种正确的方法。 Everything I try causes the application to crash. 我尝试的所有操作都会导致应用程序崩溃。

Originally I had it so the iCloud checking wasn't in a background thread and the app worked fine, but occasionally timed out. 最初我有它,所以iCloud检查不在后台线程中,并且该应用程序运行良好,但有时会超时。

You have not to know when iCloud becomes available. 您无需知道何时可以使用iCloud。 You just work with data but you don't send them directly to iCloud. 您仅处理数据,但不将其直接发送到iCloud。 iOS does it instead you. iOS代替了您。 So only it knows when and how it should send data. 因此,只有它知道何时以及如何发送数据。

No, you can't change the options on an NSPersistentStore object once it exists. 不,一旦存在NSPersistentStore对象,就无法更改其选项。 You can only specify the options when adding the persistent store to the NSPersistentStoreCoordinator . 您只能在将持久性存储添加到NSPersistentStoreCoordinator时指定选项。 The closest you could get to changing options would be to tear down the entire Core Data stack and start over with different options. 与更改选项最接近的方法是拆除整个核心数据堆栈,并从其他选项重新开始。

That wouldn't help, though, because: 但是,这没有帮助,因为:

  • Even if you have detected that iCloud is available (I'm guessing using NSFileManager , either via its ubiquityIdentityToken or by calling URLForUbiquityContainerIdentifier: ), your call to addPersistentStoreWithType:configuration:URL:options:error: might still block for a while. 即使您检测到iCloud可用(我想通过NSFileManager ,通过其ubiquityIdentityToken或调用URLForUbiquityContainerIdentifier: ,对addPersistentStoreWithType:configuration:URL:options:error:调用仍可能会阻塞一段时间。 If there's new data available in iCloud, it doesn't start downloading until you add the persistent store, and that method doesn't return until the download process is finished. 如果iCloud中有可用的新数据,则直到您添加永久存储后它才开始下载,并且直到下载过程完成后该方法才返回。 And sometimes, iCloud just makes that method block for a while for no readily apparent reason. 有时,iCloud会在没有明显原因的情况下使该方法阻塞一段时间。

  • If you let the user make any changes to the data while using non-iCloud options, those changes will not get automatically sent to the cloud later. 如果您让用户在使用非iCloud选项时对数据进行任何更改,则这些更改将不会在以后自动发送到云中。 Core Data only sends changes to iCloud when the data changes while iCloud is active-- which makes it generate a transaction. 当iCloud处于活动状态时,Core Data仅在数据发生更改时才将更改发送到iCloud,这使其生成事务。 You'd have to load and re-save any changes the user made, or those changes would never make it to the cloud. 您必须加载并重新保存用户所做的任何更改,否则这些更改将永远不会保存到云中。

You have, unfortunately, hit on one of the major stumbling points when using Core Data with iCloud. 不幸的是,将Core Data与iCloud结合使用时,您遇到了一个主要障碍。 You can't make the full data store available until Core Data finishes communicating with iCloud-- because your call to add the persistent store doesn't return until then. 在Core Data完成与iCloud的通信之前,您无法提供完整的数据存储-因为直到那时您添加持久性存储的调用都不会返回。 And you can't do anything to speed up that process. 而且您无法做任何事情来加快该过程。 This is just one of the headaches you'll run into if you continue trying to use iCloud with Core Data. 如果您继续尝试将iCloud与Core Data结合使用,这只是您头痛的麻烦之一。

Depending on the nature of your data you might be able to use two data stores, one purely local and one synced via iCloud. 根据数据的性质,您可能可以使用两个数据存储,一个是纯本地存储,另一个是通过iCloud同步的。 You could make the purely local data store available while the iCloud one tries to get its act together well enough to be useful. 您可以使纯本地数据存储可用,而iCloud试图将其行为很好地融合在一起以发挥作用。 If you stick with one data store though, you're stuck with the delay. 但是,如果您坚持使用一个数据存储,则会遇到延迟问题。

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

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