与Web服务器的Swift核心数据同步 [英] Swift Core Data Sync With Web Server

查看:384
本文介绍了与Web服务器的Swift核心数据同步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做一个应用程序(在Swift中),需要在离线和在线模式下运行。当它处于离线模式时,数据将存储在CoreData本地。一旦它检测到网络(在线),它应该与服务器同步并更新后端数据库。应该怎么走。是否有图书馆或广告连播?

I am making an app (in Swift) which needs to run in offline and online mode. When its in offline mode, data will be stored locally on CoreData. Once it detects network (online) it should sync with server and update the backend database. How should one go about it. Are there libraries or pods?

我看到这个帖子,但没有什么是可操作的。它是太高的水平,我不知道从哪里开始。我看到这个链接从 Ray Wenderlich 但不知道如何翻译成Swift。

I have seen this post but nothing is actionable. It is too high level and I am not sure where to start. I have seen this link from Ray Wenderlich but don't know how to translate to Swift.

任何教学链接也会对您有所帮助

Any tutorial links will also be helpful

推荐答案


  1. BOOL 属性添加到您的 CoreData Entity 并将其命名为已同步。这将基本上存储每个对象的同步状态。

  1. Add new BOOL attribute to your CoreData Entity and name it as synched. This will basically store the synched status for each object.

现在,您必须检查互联网连接。 请参阅此链接

Now you will have to check for internet connectivity. Refer to this link

每当您有互联网连接,只需在您的获取请求中使用以下 NSPredicate CoreData 中获取对象:

Whenever you have internet connectivity, just fetch the objects from CoreData using the following NSPredicate in your fetch request:

let isSynchedPredicate = NSPredicate(format: "synched = %@", false)


  • 现在,您只需使用您的网络服务来同步和更新所有获取的对象到服务器。在成功上传时,DONOT忘记将已同步的属性更改为 true

    这篇关于与Web服务器的Swift核心数据同步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆