Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift Core Data Sync With Web Server

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

like image 310
Anuj Arora Avatar asked Sep 16 '15 14:09

Anuj Arora


1 Answers

  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

like image 55
Nishant Avatar answered Nov 15 '22 18:11

Nishant