I recently updated my project to 0.25 from 0.20 and observed some unexpected behavior in request cancelations.
Intended outcome
Network requests on watched queries are not interrupted by cache updates.
Actual outcome
If a cache update comes in while a server fetch is in flight, the server fetch is canceled, and a .failure is sent to the completion handler.
How to reproduce the issue
- Watch a query with a cache policy that includes a server fetch
- While the server fetch is in flight, modify a dependent key in the cache in a read/write transaction
- The fetch is canceled
I believe this behavior was introduced by #1012 in 0.22
It appears that if a dependent key of the watcher is written to, a cache fetch is triggered, effectively canceling the in-flight server fetch, and issuing a .failure to the completion handler.
Here is a concrete example of how this is playing out in my project, which features a list of chat conversations:
- If some conversations are unread, the user can navigate to a carousel of unread conversations. This list of conversation was previously fetched and is available in the cache.
- When the users navigates to the carousel of unread conversations:
UnreadConversationQuery is watched with .returnCacheDataAndFetch
- The cached list is returned and the first conversation displayed. The server fetch is issued.
- The first conversation marks itself as read
isRead is set to true optimistically in the cache for this conversation
- Since
UnreadConversationQuery depends on this same isRead key, the server fetch is canceled and replaced by a cache fetch.
- The watch result handler is called with a
.failure
I recently updated my project to
0.25from0.20and observed some unexpected behavior in request cancelations.Intended outcome
Network requests on watched queries are not interrupted by cache updates.
Actual outcome
If a cache update comes in while a server fetch is in flight, the server fetch is canceled, and a
.failureis sent to the completion handler.How to reproduce the issue
I believe this behavior was introduced by #1012 in
0.22It appears that if a dependent key of the watcher is written to, a cache fetch is triggered, effectively canceling the in-flight server fetch, and issuing a
.failureto the completion handler.Here is a concrete example of how this is playing out in my project, which features a list of chat conversations:
UnreadConversationQueryis watched with.returnCacheDataAndFetchisReadis set to true optimistically in the cache for this conversationUnreadConversationQuerydepends on this sameisReadkey, the server fetch is canceled and replaced by a cache fetch..failure