简体   繁体   English

icloud核心数据同步

[英]icloud core data sync

I am starting out with building a iOS app which will utilize core data and will sync its data with other devices through iCloud. 我开始构建一个iOS应用程序,它将利用核心数据并通过iCloud将其数据与其他设备同步。 I was thinking of giving a "Sync" button so that the data is not modified at random moments by data from iCloud. 我正在考虑提供一个“同步”按钮,以便不随时通过iCloud的数据修改数据。 The following situation would then occur :- 然后会出现以下情况: -

User A is working on device A and adds two records. 用户A正在设备A上工作并添加两条记录。 User B is currently working on device B and does not press the Sync Button. 用户B当前正在使用设备B,而不是按下同步按钮。 At night, the user B decides to update the data in device B and presses the Sync Button (I have the following questions regarding this situation) :- 在晚上,用户B决定更新设备B中的数据并按下同步按钮(我对这种情况有以下问题): -

  1. Can the method mergeChangesFromContextDidSaveNotification insert those two records after the Sync button is pressed ? 在按下同步按钮后,方法mergeChangesFromContextDidSaveNotification插入这两个记录吗? (I think it can. However, just want to confirm before proceeding ahead with this model) (我认为它可以。但是,只是想在继续这个模型之前确认)
  2. Is it possible to access the properties of those two objects before inserting them into the database of device B ? 在将它们插入设备B的数据库之前,是否可以访问这两个对象的属性?

I do not think that iCloud is designed for your use case. 我不认为iCloud是专为您的用例而设计的。 iCloud syncs data across devices for the same user ie one that is logged in with the same Apple ID in more than one device. iCloud 为同一用户在设备之间同步数据,即在多个设备中使用相同Apple ID登录的数据。 The idea is that when the user opens the app on a different device, he can continue working with the data he created / modified on another device before. 这个想法是,当用户在不同的设备上打开应用程序时,他可以继续使用他之前在其他设备上创建/修改的数据。

There is quite a bit of latency in the updates, so even if you enable / disable syncing via a "Sync" button the actual delivery time is not guaranteed and can take pretty long in some cases depending on connection speeds, chance, the state of Apple servers, etc. 更新中存在相当多的延迟,因此即使您通过“同步”按钮启用/禁用同步,也无法保证实际的交付时间,并且在某些情况下可能需要很长时间,具体取决于连接速度,机会,状态Apple服务器等

Instead, you could devise a customised solution based on your own server and data scheme. 相反,您可以根据自己的服务器和数据方案设计自定义解决方案。

Since you're using iCloud with Core Data-- no, you can't do anything like this. 既然你正在使用带有核心数据的iCloud--不,你不能做这样的事情。 When iCloud syncs Core Data changes, it does so in the background. 当iCloud同步Core Data更改时,它会在后台执行此操作。 It downloads the changes, saves them to the data store, and then, after it's done it tells you about the change that has already happened. 它会下载更改,将它们保存到数据存储中,然后在完成后告诉您已经发生的更改。 Calling mergeChangesFromContextDidSaveNotification does not save the changes-- they're already saved. 调用mergeChangesFromContextDidSaveNotification不会保存更改 - 它们已经保存。 What that call does is update your managed object context with new data from the persistent store. 该调用的作用是使用持久性存储中的新数据更新托管对象上下文。 You get notified of new insertions after they have already happened. 在新的插入已经发生之后,您会收到通知。 Think of the "did import" notification as if it were a "did save" notification from another thread, telling you about something that's already finished. 想想“导入”通知,好像它是来自另一个线程的“保存”通知,告诉你已完成的事情。

Now, if you have already loaded a managed object when you receive the import notification, you could compare its current properties to those from the most recent incoming change update. 现在, 如果您在收到导入通知时已经加载了托管对象 ,则可以将其当前属性与最新的传入更改更新中的属性进行比较。 If you don't already have the object in memory, you can't get its old values, because they've already been overwritten. 如果您还没有内存中的对象,则无法获取其旧值,因为它们已被覆盖。

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

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