Swift 核心数据与 Web 服务器同步

Swift Core Data Sync With Web Server

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

我看过 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 但不知道如何翻译成 Swift。

任何教程链接也会有帮助

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

  2. 现在您必须检查 Internet 连接。 Refer to this link

  3. 只要有互联网连接,只需在获取请求中使用以下 NSPredicateCoreData 获取对象:

    let isSynchedPredicate = NSPredicate(format: "synched = %@", false)
    
  4. 现在您可以使用您的网络服务将所有获取的对象同步并更新到服务器。上传成功后,不要忘记将 synched 属性 更改为 true