How to synchronize coredata with webservices in swift?

亡梦爱人 提交于 2020-01-03 05:46:08

问题


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. please help me anyone.


回答1:


Recently, I have worked on Offline and Online mode application.

First, you have to identify the which record is added, updated and removed from each entity in Offline mode.

So to identify this, I have added one extra attribute recordStatus in every entity that required while syncing.

Your entity will look like this. I have created one ENUM that will handle all this recordStatus.

My solution is in Objective-C but I assume you can easily convert it into Swift.

typedef NS_ENUM(NSInteger, RecordStatus){

    RecordStatusUnchanged = 0,
    RecordStatusUpdated   = 1,
    RecordStatusAdded     = 2,
    RecordStatusRemoved   = 3
};

At the time of the Syncing.... You have to fetch only those records whose recordStatus != RecordStatusUnchanged and that record will be sent to the server.

I hope I am sounding clear to you.



来源:https://stackoverflow.com/questions/38888703/how-to-synchronize-coredata-with-webservices-in-swift

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