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.