Swift Core Data Sync With Web Server

早过忘川 提交于 2019-12-04 08:06:51

问题


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?

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:


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

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

  3. 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)
    
  4. Now you can just use your webservice to sync and update all the fetched objects to server. On successfull upload, DONOT forget to change the synched property to true



来源:https://stackoverflow.com/questions/32611760/swift-core-data-sync-with-web-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!