Start a new topic

Kinvey.Backbone.Collection.fetch({offline: true}) doesn't fallback to network if collection table do

I am trying to fetch a collection with the "offline: true" flag set in the hopes that it would fetch from IndexedDB if possible, but fallback to network if not. This doesn't seem to work if the collection has not previously been fetched from the network such that the table exists locally.

After debugging I can see that in IDBAdapter.find() handles the COLLECTION_NOT_FOUND case by resolving the promise with an empty array. Then higher up the stack in in the promise handler, it checks the maxAge status to determine if it should fetch from the network or not, but since the array is empty, it decides not to. Is this the intended behavior or a bug?

It seems like one solution would be to add an error handler to the promise in and let the COLLECTION_NOT_FOUND error propagate up so it can set "options.offline = false" and retry the read operation from the network.

Ok, thanks for the clarification Mark.
Yes, that statement is correct. Thanks for the feedback, the documentation will be updated to include this.
I think I understand and agree with you here. Just to confirm, you are saying the fallback option only has any effect on a record fetching level, not on entire collections? If that's correct, then Just a single line in the documentation would really help clarify this. I see now all the examples show single record interaction.

You are right: an empty array is not a failure, it simply means no models match the query / find operation you specified. The fallback operation merely applies to getting an individual model by id. If no document with the specified id exists, it is considered failure and a network request will be triggered.

It is tricky to only check for `COLLECTION_NOT_FOUND`. Imagine the following:

1. Query locally for models with `{ foo: true }`.

2. `COLLECTION_NOT_FOUND`, so query is sent over the network.

3. Cache is updated with results from 2.

4. Query locally for models with `{ foo: false }`.

5. Collection exists locally, but has no records matching the query, so empty set is returned.

6. Query is *not* sent over the network.

Step 1 and 4 should, in my eyes, follow the same steps consistently, what currently happens. Whether to send every query over the network if no records are found locally is something I feel the developer should control on a per use case basis.
Hi Mark - Sorry for my mistake here then. Looking at documentation again and it's a little unclear how the fallback option really works. Docs mention fallback determines "whether to send the request over the network if the request failed locally", and it also mentions "By default, if the model cannot be found locally, a network request will be issued to retrieve the item from the Kinvey server instead.". What exactly constitutes a failure that falls back to the network?

I understand it might be expected that no records does not mean the local request failed (and therefore shouldn't fallback to network). However the missing collection (meaning it was never fetched and cached locally) I would again assume should fallback to the network. Either way functionality seems all there, so just a little clarification on the documentation would really help here. Thanks much.
The offline flag indeed fetches everything from IndexedDB. If there are no records, an empty array will be returned. This is as expected. In your case, I’d suggest checking for this, and if the result is empty, manually trigger a network request.
Great question & findings. maxAge was just added in a recent update (v1.1.5 I believe), so chances are there is indeed a bug here. If necessary you can probably roll back to 1.1.4 in the meantime (if you don't need maxAge support), but chances are the Kinvey team will get it fixed up pretty quick.
Login or Signup to post a comment