This is a good question.
I personally think that downloading data, storing locally and then attempting to sync is a dangerous scenario. It is easy to enter errors, leading and subordinate problems (which data should be the main, if you use multiple devices, etc.)
I think something like this might be working:
1 .. I would try to look at the possibilities of lazy loading data from the server on demand. That is, when the user has a view that should display data, load that specific data with the creation of that particular view. This ensures that data is always in sync.
2. The problem of reloading data from the server from each view can be solved by simply storing the downloaded data as objects in memory (without using SqlLite). The view will try to load the necessary data through your cache manager, and it will serve it from memory, if available. If not in memory, just get the data from your server and add it to your memory cache. The memory cache can be a home data manager that wraps the Dictionary stored on you by AppDelegate , or some kind of global "Singelton" for transferring cache management, storing and loading data.
3 .. When using lazy loaded data and memory caches, you need to make sure that any updates (changes, new records, deleted records) update your memory data model and push these changes to the servers as soon as possible. Depending on the size of the data, etc. You can make the user wait or do it directly as a background process.
4. To synchronize data, you must be sure that you periodically invalid (delete) the local memory entries in the cache and thereby forcibly update data from the server. A better approach would probably be to have the latest updated timestamp for each entry in the memory cache. Thus, a periodic invalidator will only delete “old records” from the memory cache (again not from the server).
To keep the server from unnecessarily loading data, data should still be loaded on demand when the user needs it in the view, and not as part of a “cache invalidation”.
5. Depending on the size of the data, you may need a "cache invalidation". It may be as simple as when xx records are stored, start deleting old objects from the memory cache (not a server, only locally on the device).
6. If data synchronization is absolutely important, you may need to update the memory cache for writing as soon as you allow the user to modify the data. For example. when the user clicks "Change" or similar, you take the latest data from the server for this entry. This is just to make sure that the user is not going to update the record using outdated data and thereby inadvertently undo any made remote changes on another device, etc.
-
I accept it. I do not believe that there is a “perfect right path” for this. But that would be what I would like to do.
Hope this helps some ideas and inspiration.
