When my app launches, the data is often out of sync until I run the first fetch. If I close the app and relaunch, I would expect the last fetch to have been cached, but it isn't. The result is that the app loads and displays old information and after a few seconds of fetching, the results are swapped with the correct info.
If during the session the user creates/updates/deletes items in a collection that has been cached, does the cache update or do i need to run a Sync or some other function to force that changes to be cached locally?
Should I change my app to run in an offline mode by default? When it comes to a collaborative app where more than one user has read/write access to the same data, what's a best practice for managing synchronizations between users while having the immediate access of an offline datastore?
This is a bit of a loaded question, a little bit of context/example might be helpful. It sounds like you just need to get a little more specific with your caching depending on your use case. [You can control caching policies per fetch, which is very helpful for debugging.](http://devcenter.kinvey.com/backbone/guides/caching-offline#Policies "You can control caching policies per fetch, which is very helpful for debugging.")
> The result is that the app loads and displays old information and after a few seconds of fetching, the results are swapped with the correct info.
Have you inspected the network requests to see how the data is being populated? Is it being pulled from the cache initially, and then fetching the remote data? Are you seeing this happen even when the active user is not creating/updating/deleting, and just straight fetching? This is a bit unclear, as there could be a few things happening.
> If during the session the user creates/updates/deletes items in a collection that has been cached, does the cache update or do i need to run a Sync or some other function to force that changes to be cached locally?
Assuming you are in offline mode, things will only be synced when the app goes online again or you call Kinvey.Sync.execute. I would recommend using [Kinvey.Sync.count()](http://devcenter.kinvey.com/backbone/guides/caching-offline#StatusInformation "Kinvey.Sync.count()") for debugging, and [try initializing your app with sync set to offline](http://devcenter.kinvey.com/backbone/guides/caching-offline#AutomatedControl "try initializing your app with sync set to offline"). You can then investigate your local cache before triggering online manually, and watch the network requests that happen.
> When it comes to a collaborative app where more than one user has read/write access to the same data, what's a best practice for managing synchronizations between users while having the immediate access of an offline datastore?
There really isn't an all inclusive, best practice for this. It really is on a use case basis, which is why you can implement [custom conflict resolution policies.](http://devcenter.kinvey.com/backbone/guides/caching-offline#Conflicts "custom conflict resolution policies. ") I would propose entering in offline mode though with a conservative conflict resolution policy to start with.
Keep in mind that whether the user is logged in or not does have an impact on caching/sync, but your question wasn't clear if this plays into your use case. I wish I had a more definitive answer for you, but if you can provide more detail and/or example code, would be glad to continue to assist.
over 3 years ago
Thanks for the reply @OhmzTech
I'll investigate what's happening with Kinvey.Sync.count() and also try out running in offline mode. Thanks for pointing me in that direction. If things don't go as planned, I'll get a more detailed description of the problem.
over 3 years ago
Ok, here's a better explanation...
The app allows users to share lists. The list is a document in a collection 'Lists' and the items in the list are stored in a collection 'Items' with references via 'list_id'. To find the list items, we fetch for all 'Items' who's 'list_id' matches the 'Lists' _id.
The app starts up with Sync.offline() so that it reads data from the local cache and the user has an immediate experience.
The app then goes Sync.online() and re-fetches the data from the server.
The problem (not saying it's a bug, more of a workflow) that I'm seeing is that any documents that were deleted on the server (by a different user or some other process) are still located in the user's local cache. Subsequent fetches by the user update the cache with more records or changes to existing records, but they don't cause local records to be deleted because they still match the 'list_id' query and are therefore still a valid result.
The workflows that I can think of are:
1. Assign a max age to all the 'Items' and items that were deleted will eventually go away at max age time.
2. Run a Sync.destruct after before setting the Sync.online() and re-fetching the data. This will result in the brief appearance of the deleted items until the next online fetch occurs.
Am I tackling this problem correctly?
over 3 years ago
Yes, if you fetch multiple items from Kinvey, the library will not delete local items that are not part of the result set from Kinvey. Isn’t maxAge exactly what you’re looking for?
You can use `Sync.destruct` for a more real-time experience. However, I only recommend this if the amount of lists is not very big.
over 3 years ago
I think maxAge is too limiting for my needs. maxAge only updates elements when they expire but I am running a setInterval to continuously check for updates on the collection (two users sharing a grocery list, need to see each others changes every 30 seconds or so) using the last fetch response time as part of my query to only grab items that have changed. this way, i limit the amount of data received with each request. If I used maxAge with an agressive (short) lifespan, I would constantly be downloading hundreds of items.
I did notice Sync.destruct seems to take quite a while (1200 documents). I would have expected it to be quick.