问题
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:
Add new
BOOL
attribute
to yourCoreData Entity
and name it assynched
. This will basically store the synched status for each object.Now you will have to check for internet connectivity. Refer to this link
Whenever you have internet connectivity, just fetch the objects from
CoreData
using the followingNSPredicate
in your fetch request:let isSynchedPredicate = NSPredicate(format: "synched = %@", false)
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 totrue
来源:https://stackoverflow.com/questions/32611760/swift-core-data-sync-with-web-server