简体   繁体   English

在Mac和iPhone之间同步Core Data模型

[英]Syncing Core Data model between Mac and iPhone

I am currently building my Core Data model, which I would like to sync between the Mac and iPhone versions of my application. 我目前正在构建我的Core Data模型,我希望在我的应用程序的Mac和iPhone版本之间进行同步。

I will be using Bonjour for device discovery, etc but I have a question regarding the data sync part of the problem. 我将使用Bonjour进行设备发现等,但我对问题的数据同步部分有疑问。

So far I have added a UID and modification timestamp to each object that will be involved in syncing, so I should be able to match up objects and detect which ones have changed. 到目前为止,我已经为每个将参与同步的对象添加了UID和修改时间戳,因此我应该能够匹配对象并检测哪些对象已经更改。

Are there any good links, resources out there regarding writing sync code for this type of situation, ie syncing records between two instances of a model? 是否有关于为这种情况编写同步代码的良好链接,资源,即在模型的两个实例之间同步记录?

Sync is a problem with quite a few edge cases which have been solved many times by people in the past, so I was expecting to find some info on the subject but all I can find are links to Apple's SyncServices (which doesn't exist on iPhone) and some MS sync technology. 同步是一个问题,有很多边缘情况,过去人们已经解决了很多次,所以我期待找到关于这个主题的一些信息,但我能找到的只是Apple的SyncServices的链接(它不存在于iPhone)和一些MS同步技术。

I'm really looking for general theory so I can implement it myself, not necessarily a ready-made solution. 我真的在寻找一般理论,所以我自己可以实现它,不一定是现成的解决方案。

The SyncML specification may be of help, but it's quite hard to read and obviously biased towards SyncML. SyncML规范可能有所帮助,但它很难阅读,显然偏向于SyncML。

I've had to implement this for Task Coach, so here are a few ideas: 我必须为Task Coach实现这个,所以这里有一些想法:

A modification flag is enough, a timestamp doesn't really provide much more information. 修改标志就足够了,时间戳实际上并没有真正提供更多信息。 Typically, my objects are in one of these states: 通常,我的对象处于以下状态之一:

  • None 没有
  • New
  • Deleted 删除
  • Modified 改性

The following transitions happen when the object is modified: 修改对象时发生以下转换:

  • None -> Modified 无 - >修改
  • New -> New 新 - >新
  • Deleted -> (should not happen) 删除 - >(不应该发生)
  • Modified -> Modified 修改 - >修改

and the following ones when it is deleted: 以及删除时的以下内容:

  • None -> Deleted 无 - >已删除
  • New -> Actually deleted (it may be removed from storage) 新建 - >实际删除(可能会从存储中删除)
  • Deleted -> (should not happen) 删除 - >(不应该发生)
  • Modified -> Deleted 修改 - >删除

When synchronizing, the device first sends to the desktop all objects with a status different than None. 同步时,设备首先向桌面发送状态不同于None的所有对象。 The desktop asks the user to resolve conflicts if one of these has a status != None on its side. 如果其中一个状态为!=无,则桌面会要求用户解决冲突。 In any case, the object goes into state None on the device, or is deleted from storage if its state was Deleted. 在任何情况下,对象在设备上进入状态None,或者如果状态为Deleted则从存储中删除。

Then, the desktop sends its own changes to the device. 然后,桌面会将自己的更改发送到设备。 There are no conflicts possible since all objects are in state None on the device. 由于设备上的所有对象都处于无状态,因此不存在冲突。 Objects on the desktop go into state None or are deleted from storage as well, and sync is over. 桌面上的对象进入状态无或从存储中删除,同步结束。

There are two types of possible conflicts, depending on the device/desktop states: 根据设备/桌面状态,有两种类型的可能冲突:

  • modified/deleted. 改性/删除。 If the user chooses to trust the device, the desktop object is replaced with the device one; 如果用户选择信任该设备,则将桌面对象替换为设备对象; else, the desktop does nothing and keeps the deleted state, so that the object will be removed from the device in phase 2. 否则,桌面不会执行任何操作并保持已删除状态,以便在第2阶段将对象从设备中删除。
  • deleted/modified: If the device wins, the object is actually deleted from the desktop. 删除/修改:如果设备获胜,则实际从桌面删除该对象。 Else, the object goes into state New on the desktop so that it is restored on the device in phase 2. 否则,对象将进入桌面状态New,以便在阶段2中在设备上恢复。
  • deleted/deleted: Duh. 删除/删除:Duh。 Just remove it from storage. 只需将其从存储中删除即可
  • modified/modified: The user decides which values to keep, maybe on a field by field basis. 修改/修改:用户可以逐个字段地决定要保留哪些值。 The state stays to Modified on the desktop so that these choices are propagated back to the device in phase 2. 状态在桌面上保持为“已修改”,以便在第2阶段将这些选项传播回设备。

Some conflicts may be avoided if the Modified state is kept for each field, so that for instance an object with a modified Subject on the device and modified Summary on the desktop will not trigger a conflict. 如果为每个字段保留Modified状态,则可以避免某些冲突,例如,设备上具有已修改Subject的对象和桌面上已修改的Summary将不会触发冲突。

You may take a look at the code for Task Coach for an example (SVN repository on SourceForge, it has both the desktop app in Python and the iPhone app). 您可以查看Task Coach的代码示例(SourceForge上的SVN存储库,它同时包含Python中的桌面应用程序和iPhone应用程序)。 Actually, in this case I decided to use a simpler approach; 实际上,在这种情况下,我决定使用更简单的方法; I don't keep track of the state on the desktop. 我没有跟踪桌面上的状态。 After phase 1 (device to desktop), I just make a full replacement of objects on the device with the ones on the desktop. 在第1阶段(设备到桌面)之后,我只需用桌面上的对象完全替换设备上的对象。 Thus, there are no conflicts (the device always wins). 因此,没有冲突(设备总是赢)。

Obviously, this only works between two fixed devices; 显然,这仅适用于两个固定设备之间; if you want to synchronize with several phones/desktop apps, you have to assign a unique ID to each and keep different states for different devices/apps. 如果要与多个电话/桌面应用同步,则必须为每个应用分配唯一的ID,并为不同的设备/应用保留不同的状态。 This may begin to get hairy. 这可能会变得毛茸茸。

HTH HTH

Marcus Zarra created a framework called ZSync to simplify synching iPhone/iPad apps to their Mac counterparts. Marcus Zarra创建了一个名为ZSync的框架,以简化iPhone / iPad应用程序与Mac同行的同步。 Take a look at it, it may help solve the problem. 看看它,它可能有助于解决问题。

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

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