本文翻译自:How to Sync iPhone Core Data with web server, and then push to other devices? [closed]

I have been working on a method to sync core data stored in an iPhone application between multiple devices, such as an iPad or a Mac. 我一直在研究一种方法,用于在多个设备(如iPad或Mac)之间同步存储在iPhone应用程序中的核心数据。 There are not many (if any at all) sync frameworks for use with Core Data on iOS. 在iOS上使用Core Data的同步框架并不多(如果有的话)。 However, I have been thinking about the following concept: 但是,我一直在考虑以下概念:

  1. A change is made to the local core data store, and the change is saved. 对本地核心数据存储进行更改,并保存更改。 (a) If the device is online, it tries to send the changeset to the server, including the device ID of the device which sent the changeset. (a)如果设备在线,它会尝试将变更集发送到服务器,包括发送变更集的设备的设备ID。 (b) If the changeset does not reach the server, or if the device is not online, the app will add the change set to a queue to send when it does come online. (b)如果变更集未到达服务器,或者设备未联机,则应用程序会将更改集添加到队列中以在其联机时发送。
  2. The server, sitting in the cloud, merges the specific change sets it receives with its master database. 位于云中的服务器将其接收的特定更改集与其主数据库合并。
  3. After a change set (or a queue of change sets) is merged on the cloud server, the server pushes all of those change sets to the other devices registered with the server using some sort of polling system. 在云服务器上合并更改集(或更改集队列)后,服务器会使用某种轮询系统将所有这些更改集推送到向服务器注册的其他设备。 (I thought to use Apple's Push services, but apparently according to the comments this is not a workable system.) (我想使用Apple的推送服务,但显然根据评论,这不是一个可行的系统。)

Is there anything fancy that I need to be thinking about? 我需要考虑什么花哨的东西吗? I have looked at REST frameworks such as ObjectiveResource , Core Resource , and RestfulCoreData . 我查看了REST框架,例如ObjectiveResource , Core Resource和RestfulCoreData 。 Of course, these are all working with Ruby on Rails, which I am not tied to, but it's a place to start. 当然,这些都与Ruby on Rails一起使用,我并不依赖它,但它是一个开始的地方。 The main requirements I have for my solution are: 我对我的解决方案的主要要求是:

  1. Any changes should be sent in the background without pausing the main thread. 任何更改都应该在后台发送而不会暂停主线程。
  2. It should use as little bandwidth as possible. 它应该使用尽可能少的带宽。

I have thought about a number of the challenges: 我考虑过一些挑战:

  1. Making sure that the object IDs for the different data stores on different devices are attached on the server. 确保服务器上附加了不同设备上不同数据存储的对象ID。 That is to say, I will have a table of object IDs and device IDs, which are tied via a reference to the object stored in the database. 也就是说,我将有一个对象ID和设备ID表,它们通过对存储在数据库中的对象的引用来绑定。 I will have a record (DatabaseId [unique to this table], ObjectId [unique to the item in the whole database], Datafield1, Datafield2), the ObjectId field will reference another table, AllObjects: (ObjectId, DeviceId, DeviceObjectId). 我将有一条记录(DatabaseId [此表是唯一的],ObjectId [整个数据库中的项目唯一],Datafield1,Datafield2),ObjectId字段将引用另一个表AllObjects:(ObjectId,DeviceId,DeviceObjectId)。 Then, when the device pushes up a change set, it will pass along the device Id and the objectId from the core data object in the local data store. 然后,当设备推送更改集时,它将从本地数据存储中的核心数据对象传递设备Id和objectId。 Then my cloud server will check against the objectId and device Id in the AllObjects table, and find the record to change in the initial table. 然后我的云服务器将检查AllObjects表中的objectId和设备ID,并找到要在初始表中更改的记录。
  2. All changes should be timestamped, so that they can be merged. 所有更改都应加上时间戳,以便合并它们。
  3. The device will have to poll the server, without using up too much battery. 设备必须轮询服务器,而不会耗尽太多电池。
  4. The local devices will also need to update anything held in memory if/when changes are received from the server. 如果/从服务器收到更改,本地设备还需要更新内存中保存的任何内容。

Is there anything else I am missing here? 还有什么我在这里失踪的吗? What kinds of frameworks should I look at to make this possible? 我应该考虑哪些框架才能实现这一目标?


#1楼

参考:https://stackoom.com/question/L7ro/如何将iPhone核心数据与Web服务器同步-然后推送到其他设备-关闭


#2楼

I have just posted the first version of my new Core Data Cloud Syncing API, known as SynCloud. 我刚刚发布了我的新Core Data Cloud Syncing API的第一个版本,称为SynCloud。 SynCloud has a lot of differences with iCloud because it allows for Multi-user sync interface. SynCloud与iCloud有很多不同之处,因为它允许多用户同步界面。 It is also different from other syncing api's because it allows for multi-table, relational data. 它也与其他同步api不同,因为它允许多表关系数据。

Please find out more at http://www.syncloudapi.com 请在http://www.syncloudapi.com上了解更多信息

Build with iOS 6 SDK, it is very up to date as of 9/27/2012. 使用iOS 6 SDK构建,它是截至2012年9月27日的最新版本。


#3楼

Similar like @Cris I've implemented class for synchronization between client and server and solved all known problems so far (send/receive data to/from server, merge conflicts based on timestamps, removed duplicate entries in unreliable network conditions, synchronize nested data and files etc .. ) 类似于@Cris我实现了客户端和服务器之间的同步类,并解决了目前为止所有已知的问题(向/从服务器发送/接收数据,基于时间戳合并冲突,在不可靠的网络条件下删除重复条目,同步嵌套数据和文件等..)

You just tell the class which entity and which columns should it sync and where is your server. 您只需告诉类哪个实体和哪些列应该同步以及服务器在哪里。

M3Synchronization * syncEntity = [[M3Synchronization alloc] initForClass: @"Car"andContext: contextandServerUrl: kWebsiteUrlandServerReceiverScriptName: kServerReceiverScriptandServerFetcherScriptName: kServerFetcherScriptansSyncedTableFields:@[@"licenceNumber", @"manufacturer", @"model"]andUniqueTableFields:@[@"licenceNumber"]];syncEntity.delegate = self; // delegate should implement onComplete and onError methods
syncEntity.additionalPostParamsDictionary = ... // add some POST params to authenticate current user[syncEntity sync];

You can find source, working example and more instructions here: github.com/knagode/M3Synchronization . 您可以在此处找到源代码,工作示例和更多说明: github.com/knagode/M3Synchronization 。


#4楼

First you should rethink how many data, tables and relations you will have. 首先,您应该重新考虑您将拥有多少数据,表格和关系。 In my solution I've implemented syncing through Dropbox files. 在我的解决方案中,我已经通过Dropbox文件实现了同步。 I observe changes in main MOC and save these data to files (each row is saved as gzipped json). 我观察主MOC中的更改并将这些数据保存到文件中(每行保存为gzip压缩包)。 If there is an internet connection working, I check if there are any changes on Dropbox (Dropbox gives me delta changes), download them and merge (latest wins), and finally put changed files. 如果有互联网连接工作,我检查Dropbox是否有任何变化(Dropbox给我增量更改),下载它们并合并(最新的胜利),最后放置更改的文件。 Before sync I put lock file on Dropbox to prevent other clients syncing incomplete data. 在同步之前,我将锁定文件放在Dropbox上,以防止其他客户端同步不完整的数据。 When downloading changes it's safe that only partial data is downloaded (eg lost internet connection). 下载更改时,只能下载部分数据(例如丢失的互联网连接),这是安全的。 When downloading is finished (fully or partial) it starts to load files into Core Data. 下载完成后(完全或部分),它开始将文件加载到Core Data中。 When there are unresolved relations (not all files are downloaded) it stops loading files and tries to finish downloading later. 当有未解决的关系(并非所有文件都被下载)时,它会停止加载文件并尝试稍后完成下载。 Relations are stored only as GUID, so I can easly check which files to load to have full data integrity. 关系仅作为GUID存储,因此我可以轻松检查要加载哪些文件以获得完整的数据完整性。 Syncing is starting after changes to core data are made. 在对核心数据进行更改后,将开始同步。 If there are no changes, than it checks for changes on Dropbox every few minutes and on app startup. 如果没有变化,则每隔几分钟和应用启动时检查Dropbox上的更改。 Additionaly when changes are sent to server I send a broadcast to other devices to inform them about changes, so they can sync faster. 另外,当更改发送到服务器时,我会向其他设备发送广播以通知他们有关更改,以便他们可以更快地进行同步。 Each synced entity has GUID property (guid is used also as a filename for exchange files). 每个同步的实体都有GUID属性(guid也用作交换文件的文件名)。 I have also Sync database where I store Dropbox revision of each file (I can compare it when Dropbox delta resets it's state). 我还有Sync数据库,我存储每个文件的Dropbox版本(我可以在Dropbox delta重置它的状态时进行比较)。 Files also contain entity name, state (deleted/not deleted), guid (same as filename), database revision (to detect data migrations or to avoid syncing with never app versions) and of course the data (if row is not deleted). 文件还包含实体名称,状态(已删除/未删除),guid(与文件名相同),数据库修订(用于检测数据迁移或避免与从不应用程序版本同步)以及数据(如果未删除行)。

This solution is working for thousands of files and about 30 entities. 该解决方案适用于数千个文件和大约30个实体。 Instead of Dropbox I could use key/value store as REST web service which I want to do later, but have no time for this :) For now, in my opinion, my solution is more reliable than iCloud and, which is very important, I have full control on how it's working (mainly because it's my own code). 而不是Dropbox我可以使用键/值存储作为REST Web服务,我想稍后做,但没有时间这个:)现在,在我看来,我的解决方案比iCloud更可靠,这是非常重要的,我完全控制它是如何工作的(主要是因为它是我自己的代码)。

Another solution is to save MOC changes as transactions - there will be much less files exchanged with server, but it's harder to do initial load in proper order into empty core data. 另一个解决方案是将MOC更改保存为事务 - 与服务器交换的文件将会少得多,但是以正确的顺序对空核心数据进行初始加载会更加困难。 iCloud is working this way, and also other syncing solutions have similar approach, eg TICoreDataSync . iCloud正在以这种方式工作,其他同步解决方案也有类似的方法,例如TICoreDataSync 。

-- UPDATE - 更新

After a while, I migrated to Ensembles - I recommend this solution over reinventing the wheel. 过了一会儿,我迁移到了Ensembles - 我推荐这个解决方案而不是重新发明轮子。


#5楼

I think a good solution to the GUID issue is "distributed ID system". 我认为GUID问题的一个很好的解决方案是“分布式ID系统”。 I'm not sure what the correct term is, but I think that's what MS SQL server docs used to call it (SQL uses/used this method for distributed/sync'ed databases). 我不确定正确的术语是什么,但我认为这是MS SQL服务器文档用来调用它的(SQL使用/使用此方法用于分布式/同步数据库)。 It's pretty simple: 这很简单:

The server assigns all IDs. 服务器分配所有ID。 Each time a sync is done, the first thing that is checked are "How many IDs do I have left on this client?" 每次完成同步时,首先检查的是“我在此客户端上剩下多少个ID?” If the client is running low, it asks the server for a new block of IDs. 如果客户端运行不足,则会向服务器请求新的ID块。 The client then uses IDs in that range for new records. 然后,客户端将该范围内的ID用于新记录。 This works great for most needs, if you can assign a block large enough that it should "never" run out before the next sync, but not so large that the server runs out over time. 这适用于大多数需求,如果你可以分配一个足够大的块,它应该“永远”在下一次同步之前耗尽,但不要太大以至于服务器随着时间的推移而耗尽。 If the client ever does run out, the handling can be pretty simple, just tell the user "sorry you cannot add more items until you sync"... if they are adding that many items, shouldn't they sync to avoid stale data issues anyway? 如果客户端用完了,处理可能非常简单,只需告诉用户“抱歉,在您同步之前无法添加更多项目”......如果他们添加了很多项目,则不应同步以避免陈旧数据问题无论如何?

I think this is superior to using random GUIDs because random GUIDs are not 100% safe, and usually need to be much longer than a standard ID (128-bits vs 32-bits). 我认为这优于使用随机GUID,因为随机GUID不是100%安全的,并且通常需要比标准ID(128位对32位)长得多。 You usually have indexes by ID and often keep ID numbers in memory, so it is important to keep them small. 您通常按ID分配索引,并且通常会将ID号保留在内存中,因此将它们保持在较小的位置非常重要。

Didn't really want to post as answer, but I don't know that anyone would see as a comment, and I think it's important to this topic and not included in other answers. 不是真的想发帖作为答案,但我不知道有人会看到评论,我认为这对于这个话题很重要,而不包括在其他答案中。


#6楼

2017 2017年

Regarding this incredibly old question. 关于这个难以置信的老问题。

It would be very much like asking 这非常像问

"I want to buy a device which is a phone that I can carry with me - but also use for many computing tasks, even browsing the WWW!" “我想购买一台可随身携带的手机 - 但也可用于许多计算任务,甚至可以浏览WWW!”

Obviously, the answer to that one is if you've been on Mars, one of the main technologies realized on this planet recently was "smart phones", buy one. 显然,对于那个问题的答案是, 如果你曾经在火星上,最近在这个星球上实现的主要技术之一是“智能手机”,买一个。

These days, creating an OCC system from scratch would be as insane as creating an SQL database from scratch. 目前,从头开始创建OCC系统与从头创建SQL数据库一样疯狂。

Obviously, for OCC, which is the base paradigm of all non-trivial apps now, you use 显然,对于OCC来说,这是现在所有非平凡应用程序的基本范例,您可以使用

  • Firebase 火力地堡
  • PubNub PubNub
  • Couchbase Couchbase

and so on, which are quite simply, the major advance in human technology of the last few years . 等等,这很简单,是过去几年人类技术的重大进步 。

Today, you would no more create OCC from scratch than you would 今天,你不会再从头开始创建OCC了

  • write your own operating system from scratch 从头开始编写自己的操作系统

  • write your own SQL database from scratch 从头开始编写自己的SQL数据库

  • write your own font-rendering from scratch 从头开始编写自己的字体渲染

Note that indeed, in a professional sense you can't be "an ios programmer" or "an android programmer" any more. 请注意,实际上,在专业意义上,你不能再是“ios程序员”或“Android程序员”。

Who cares about knowing how to layout tables and buttons? 谁在乎知道如何布置表格和按钮?

You're a Firebase/whatever expert, and as an incidental side issue you know how to layout buttons, etc on ios or android. 你是一个Firebase /无论专家,作为一个偶然的问题,你知道如何在ios或android上布局按钮等。

The only issue is which BAAS to use - for example, maybe PlayFab if it is game oriented, maybe PubNub if it is really message driven, maybe ably.io, maybe kinvey if you're corporate - whatever. 唯一的问题是使用BAAS - 例如,如果它是面向游戏的话可能是PlayFab,如果它真的是消息驱动的话可能是PubNub,也许是ably.io,如果你是公司的话可能是kinvey - 无论如何。

如何将iPhone核心数据与Web服务器同步,然后推送到其他设备? [关闭]相关推荐

  1. 关于web端的消息推送方式转载

    引言: 在互联网高速发展的时代里,web应用大有取代桌面应用的趋势,不必再去繁琐的安装各种软件,只需一款主流浏览器即可完成大部分常规操作,这些原因都在吸引着软件厂商和消费者.而随着各大厂商浏览器版本的 ...

  2. WEB即时通讯/消息推送

    写在前面 通常进行的Web开发都是由客户端主动发起的请求,然后服务器对请求做出响应并返回给客户端.但是在很多情况下,你也许会希望由服务器主动对客户端发送一些数据. 那么,该如何实现这个需求,或者说该如 ...

  3. firebase 推送_如何使用Firebase向Web应用程序添加推送通知?

    firebase 推送 by Leonardo Cardoso 由莱昂纳多·卡多佐(Leonardo Cardoso) 如何使用Firebase向Web应用程序添加推送通知? (How to add ...

  4. php 通知客户端,PHP+SSE服务器向客户端推送消息

    SSE与WebSocket作用相似,都是建立浏览器与服务器之间的通信渠道,然后服务器向浏览器推送信息. 但是WebSocket比SSE强大很多,SSE只能作为一个轻量级的消息推送方案,解决了从服务端向 ...

  5. go语言消息推送服务器,Golang 消息推送系统

    ## Golang 消息推送系统 - 使用 websocket 协议,实现全局推送,和分房间推送,实现用户加入房间,退出房间 - gateway 长连接网关服务器,管理全部的链接,以及房间等相关操作 ...

  6. 微信公众号-java上传图片素材到微信服务器基于事件推送两条消息

    微信公众号-java上传图片素材到微信服务器&基于事件推送两条消息 场景:基于微信扫码登录/关注事件,向用户推送两条消息. 分析:翻阅了微信开发者文档和一些其他资料发现,公众号针对用户关注/扫 ...

  7. js轮询导致服务器瘫痪_演进:Tengine 从 Web 代理服务器 到 分布式推送服务器

    Tengine Tengine 作为代理服务器,在集团有着广泛的应用基础,从 部署在 应用单机上的 Tengine ,到作为集群式部署的统一接入 Aserver ,可以说集团几乎所有应用机器均运行着 ...

  8. iphone怎么更新9.0系统更新服务器,iOS 9 推送前你必须知道的几件事:iOS 9 升级指南...

    iOS 9 即将在美国时间的 9 月 16 日推送,算下来应该是中国这边的 17 日周四凌晨 1:00.不管怎么样,17 号早上一醒来,妥妥就可以更新了. 这一次更新带来了数项新功能和优化.一套全新的 ...

  9. VS2015调试时没有启动IIS Express Web服务器 或者停止调试时 IIS Express 跟着关闭

    解决方法: 打开 解决方案资源管理器 -> 点选 Web 项目选择 -> 属性 -> Web "服务器"  去掉勾选"将服务器设置应道所有用户" ...

最新文章

  1. CSS 定位 (Positioning)
  2. 17.1.1.3 Creating a User for Replication
  3. adnroid开发环境的搭建
  4. ELK学习4_Elasticsearch+Logstash+Kibana安装_简易版
  5. ios 重复引用 静态库_iOS-如果静态库(.a)包含重复的目标文件(.o),会发生什么?
  6. Mockito框架研究 - how is match any string implemented
  7. python识别pdf文字_Python 神工具包!翻译、文字识别、语音转文字统统搞定
  8. Oracle expdp 导出
  9. PCM(脉冲编码调制)、iLBC编解码、opus(声音编码格式)、VP8视频压缩格式、H.264数字视频压缩格式
  10. 康德的道德观与哲学观
  11. pb 数据窗口 *号隐藏_王者荣耀背后的腾讯自研数据库TcaplusDB实践
  12. 色彩特征提取-色彩属性HSV空间
  13. 鸿蒙开发板hi3861硬件SPI通信arduinoUNO
  14. 小米手机无限重启怎么办?
  15. python判断是否为英文_Python判断字符串是否纯英文(纯ASCII码字符)
  16. C#对Dictionary的按Value排序
  17. Window10下解决弹出兼容性助手对话框的方法
  18. 高等数学学习笔记——第三讲——函数的概念与性质(1. 函数的概念)
  19. 都2021年了,你还不知道混沌工程( Chaos Engineering)?
  20. App跨平台开发方案与抉择

热门文章

  1. [肖博数学干货]高考数学二轮复习方法之概率和统计附强化题型解析
  2. 全屏状态下的ESC键监听处理
  3. RK3399 ARM开发板添加中文输入法
  4. CRAFT: Character Region Awareness for Text Detection ---- 论文阅读笔记
  5. Unable to determine the relationship represented by navigation ‘XXX‘ of type ‘XXX‘.
  6. 朋友们求助一个相关实际操作中的设计的问题
  7. JavaScript_第一天
  8. Lynis介绍与使用
  9. 【Java 8 新特性】Java 8 Collectors:joining()使用示例
  10. maven打包报错 Failed to execute goal org.apache.maven.plugins:maven-jar-plugin:3.0.2:jar