简体   繁体   English

使用Core Data + iCloud sync时在应用程序和Today小部件之间共享数据的正确方法

[英]Correct way to share data between app and Today widget when using Core Data + iCloud sync

I have an app with a Today widget extension. 我有一个带有Today小部件扩展名的应用。 I need the app, widget, and other instances of the app and widget on other devices to all share data, so I'm using iCloud Core Data integration. 我需要应用程序,小部件以及其他设备上的应用程序和小部件的其他实例才能共享数据,因此我正在使用iCloud Core Data集成。 At first, I had the app and widget (within a single device) sharing the same Core Data store (sqlite) in a shared container directory. 首先,我在共享容器目录中将应用程序和小部件(在单个设备中)共享相同的Core Data存储(sqlite)。 Now I've enabled iCloud syncing, and that's working between devices, but now I'm getting inconsistent data shown between the app and the widget on the same device (?!?). 现在,我已经启用了iCloud同步,并且可以在设备之间运行,但是现在我在同一设备上的应用程序和小部件之间显示的数据不一致(?!?)。 The problem could be with my widget GUI update cycle or Notification Center subscriptions, which I'm still troubleshooting, but while I do that: 问题可能出在我的窗口小部件GUI更新周期或Notification Center订阅上,我仍在对其进行故障排除,但是当我这样做时:

What is the "right" solution for sharing data from Core Data between an app and a widget (or two apps I suppose) on the same device when you're using Core Data + iCloud? 当您使用Core Data + iCloud时,在同一设备上的一个应用程序和一个小部件(或两个我假定为两个应用程序)之间共享来自Core Data数据的“正确”解决方案是什么? Should they share a single physical store, or should they each have their own store? 他们应该共享一个实体商店,还是每个人都有自己的商店? Are there gotchas to either if both approaches are valid? 如果这两种方法都有效,有什么陷阱吗?

The first question you should consider is whether you really need to share the whole Core Data store with your extension. 您应该考虑的第一个问题是,您是否真的需要与扩展共享整个Core Data存储。 If it is possible just to share a little data, perhaps via a plist, it will make things quite a bit simpler. 如果有可能仅通过plist共享少量数据,它将使事情变得更加简单。

Assuming this is not an option, and you really do need the whole store, you have two options: 假设这不是一个选择,并且您确实需要整个商店,则有两个选择:

  1. Share a single store in the shared container 在共享容器中共享一个商店
  2. Adopt a Core Data sync solution, and have separate stores for your main app and extension. 采用Core Data同步解决方案,并为您的主要应用和扩展程序设置单独的存储区。

It is possible to share a single store between two separate processes, but there are some gotchas. 可以在两个单独的进程之间共享一个商店,但是有些陷阱。 If one process saves, any NSManagedObjectContext in the second process will not register the changes, which will mean the data will not update in the UI, and could also lead to a failed save later. 如果保存了一个进程, NSManagedObjectContext第二个进程中的任何NSManagedObjectContext都将不会注册更改,这意味着数据将不会在UI中更新,并且也可能导致稍后保存失败。

To get around this, you would have to find a way to pass the object ids of the changed objects from one process to the other, so that the receiving process can refresh objects and re-fetch the latest data. 要解决此问题,您将必须找到一种方法,将更改的对象的对象ID从一个进程传递到另一个进程,以便接收进程可以刷新对象并重新获取最新数据。

The second option would involve two separate stores, and a means of transferring changes. 第二种选择将涉及两个单独的存储,以及一种转移变更的方法。 If you go with iCloud + Core Data, iCloud is the transfer mechanism, which is a bit overkill given that your processes are both on the same device. 如果您使用iCloud +核心数据,则iCloud是传输机制,考虑到您的进程都在同一设备上,这有点过分。

I believe Apple have even warned against using this solution on iOS, because if an iOS app goes to the background, it is possible it will be stopped while a file coordinator has a lock on a file, and that can cause deadlocks in the other process. 我相信Apple甚至警告过不要在iOS上使用此解决方案,因为如果iOS应用程序进入后台,则有可能在文件协调器锁定文件时将其停止,这可能导致其他进程死锁。

iCloud + Core Data is not the only sync solution. iCloud + Core Data不是唯一的同步解决方案。 A better solution in this case may be the Ensembles framework, which I develop. 在这种情况下,一个更好的解决方案可能是我开发的Ensembles框架。 The reason I say that is that it has the option to sync via local files, rather than requiring a cloud service. 我之所以这样说,是因为它可以选择通过本地文件进行同步,而不需要云服务。 You could setup separate stores for your extension and main app, and have Ensembles use the shared container for transferring change sets. 您可以为扩展程序和主应用程序设置单独的存储,并使Ensembles使用共享容器来传输更改集。 No cloud involvement, and no file coordination issues. 不涉及云,也没有文件协调问题。

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

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