Swift Core Data Sync With Web Server
Asked Answered
M

1

13

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

Mucilaginous answered 16/9, 2015 at 14:40 Comment(0)
T
11
  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

Tympany answered 16/9, 2015 at 14:50 Comment(8)
How does one ensure that each time the app does not upload everything to server..ideally it should only upload/ download data which is not in sync with server..Mucilaginous
This method will ensure that the objects with synched = false, only those are updated to server. If no such object exists, then there is no need to hit the update-webservice. Again, I stress on the fact that after a set of objects are uploaded, then you should set the synched attribute to false for all those objects.Tympany
by any chance do you have a sample Xcode project which incorporates this..will be great if you can share...Mucilaginous
@AnujArora: Sorry dude, I don't have any project handy to show this implementation, and its too-big-a-task to make a sample project for this. You can try writing your code and in case you face any issues, you can get back to me with a new question. Happy to help.Tympany
Fair enough..quick question...how do you keep checking the internet availablity all the time in background. I have seen the link you posted..but lets say I am on a view..the internet is switched on, off & again on..where will the check be made..in view_did_load..? im guessing if the internet keeps switching on/ off..the "synched" value will have to be changed for all objectsMucilaginous
check out this implementation of AFNetworking for notifying on connectivity status change: https://mcmap.net/q/567843/-afnetworking-and-no-internet-connection-scenario/1463604Tympany
@Tympany I understand this question is fairly old but I am trying to achieve the same result where data is synched to a server once there is internet connection. I am not sure what you mean on step 3. Is there a way you could elaborate that further? If this is something that goes off the context of this question then let me know and I will ask a question a new question with explanation of my approach so far.Edison
What if deleted rows?Becka

© 2022 - 2024 — McMap. All rights reserved.