-
Notifications
You must be signed in to change notification settings - Fork 749
Do not cancel server fetch of watcher on dependent key update #1156
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 8 commits
08c4a62
84c56e6
08febcb
ca136d7
ef825e3
3899f9d
2b07e69
bcbb46f
c3f1945
9ffce33
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -35,11 +35,14 @@ public final class GraphQLQueryWatcher<Query: GraphQLQuery>: Cancellable, Apollo | |
| fetch(cachePolicy: .fetchIgnoringCacheData) | ||
| } | ||
|
|
||
| // Watchers always call result handlers on the main queue. | ||
| private let queue: DispatchQueue = .main | ||
|
|
||
| func fetch(cachePolicy: CachePolicy) { | ||
| // Cancel anything already in flight before starting a new fetch | ||
| fetching?.cancel() | ||
| fetching = client?.fetch(query: query, cachePolicy: cachePolicy, context: &context, queue: .main) { [weak self] result in | ||
| guard let `self` = self else { return } | ||
| fetching = client?.fetch(query: query, cachePolicy: cachePolicy, context: &context, queue: queue) { [weak self] result in | ||
| guard let self = self else { return } | ||
|
|
||
| switch result { | ||
| case .success(let graphQLResult): | ||
|
|
@@ -66,7 +69,21 @@ public final class GraphQLQueryWatcher<Query: GraphQLQuery>: Cancellable, Apollo | |
| guard let dependentKeys = dependentKeys else { return } | ||
|
|
||
| if !dependentKeys.isDisjoint(with: changedKeys) { | ||
| fetch(cachePolicy: .returnCacheDataElseFetch) | ||
| // First, attempt to reload the query from the cache directly, in order not to interrupt any in-flight server-side fetch. | ||
| store.load(query: query) { [weak self] result in | ||
| guard let self = self else { return } | ||
|
|
||
| switch result { | ||
| case .success(let graphQLresult): | ||
| self.queue.async { | ||
| self.dependentKeys = graphQLresult.dependentKeys | ||
|
designatednerd marked this conversation as resolved.
Outdated
|
||
| self.resultHandler(result) | ||
| } | ||
| case .failure: | ||
| // If the cache fetch is not successful, for instance if the data is missing, refresh from the server. | ||
| self.fetch(cachePolicy: .fetchIgnoringCacheData) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Before it was
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also the |
||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.