How to synchronize coredata with webservices in sw

2020-08-01 05:49发布

问题:

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.



标签: swift swift2