Skip to content

Cache write on watcher dependent key can cancel server fetch #1155

@flobories

Description

@flobories

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions