For the needs of a project, I would persist the datas contained in Core Data in several places depending on the state of the user.
- If the user is logged to my API -> Persist the datas on my API.
- If the is logged on iCloud -> Persist the datas on iCloud
- In all case -> Persist the datas on the device.
Indeed, with this schema, the datas could be saved on the device, on iCloud and on my API. We need to duplicate datas to be able to keep the last datas avalaible even if the user is not logged on iCloud or the API.
Here is an implementation of how I think this feature will be implemented.
To uniformise the process of executing CRUD requests I think to
PersistenceManager which permits to execute CRUD requests for entities depending the sate of the user. (If he's logged on the API or on iCloud or nowhere).
Here there are some methods that the
PersistenceManager will implement:
This method will save an entity depending on the state of the user.
If the user is logged to the API: persist on the API If the user is logged to iCloud: save on iCloud In all case -> Save on the device with Core Data.
Each datas stored will have a timestamp to be sure to get the last version of the data.
This method will retrieve entities only from de device in asynchronous mode.
For each Save, Update or Delete request executed, if the user is logged to the API or with iCloud, one or both will send a notification to clients application where the user is logged to retrieve datas from API or iCloud and store them into Core Data.
Then the data that the user want to read is returned by Core Data.
To retrieve notification from the API, I think use socket through my API and my iOS app to communicate. But I don't know how can I do the same mechanism for iCloud. Any suggestion ?
Same mechanical than for the save request but for update datas.
Same mechanical than for the save request but for delete datas.
I'm not familiar with these problematics, and I would like have your advices on it.
Thanks a lot !