Skip to content
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

Allow callbackQueue to be specified for GraphQLWatcher #1723

Merged
merged 2 commits into from
Mar 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Sources/Apollo/ApolloClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ extension ApolloClient: ApolloClientProtocol {
@discardableResult public func fetch<Query: GraphQLQuery>(query: Query,
cachePolicy: CachePolicy = .returnCacheDataElseFetch,
contextIdentifier: UUID? = nil,
queue: DispatchQueue = DispatchQueue.main,
queue: DispatchQueue = .main,
resultHandler: GraphQLResultHandler<Query.Data>? = nil) -> Cancellable {
return self.networkTransport.send(operation: query,
cachePolicy: cachePolicy,
Expand All @@ -100,9 +100,11 @@ extension ApolloClient: ApolloClientProtocol {

public func watch<Query: GraphQLQuery>(query: Query,
cachePolicy: CachePolicy = .returnCacheDataElseFetch,
callbackQueue: DispatchQueue = .main,
resultHandler: @escaping GraphQLResultHandler<Query.Data>) -> GraphQLQueryWatcher<Query> {
let watcher = GraphQLQueryWatcher(client: self,
query: query,
callbackQueue: callbackQueue,
resultHandler: resultHandler)
watcher.fetch(cachePolicy: cachePolicy)
return watcher
Expand Down
8 changes: 5 additions & 3 deletions Sources/Apollo/ApolloClientProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public protocol ApolloClientProtocol: class {
/// - Parameters:
/// - query: The query to fetch.
/// - cachePolicy: A cache policy that specifies when results should be fetched from the server and when data should be loaded from the local cache.
/// - queue: A dispatch queue on which the result handler will be called. Defaults to the main queue.
/// - queue: A dispatch queue on which the result handler will be called. Should default to the main queue.
/// - contextIdentifier: [optional] A unique identifier for this request, to help with deduping cache hits for watchers. Should default to `nil`.
/// - resultHandler: [optional] A closure that is called when query results are available or when an error occurs.
/// - Returns: An object that can be used to cancel an in progress fetch.
Expand All @@ -37,18 +37,20 @@ public protocol ApolloClientProtocol: class {
/// - Parameters:
/// - query: The query to fetch.
/// - cachePolicy: A cache policy that specifies when results should be fetched from the server or from the local cache.
/// - callbackQueue: A dispatch queue on which the result handler will be called. Should default to the main queue.
/// - resultHandler: [optional] A closure that is called when query results are available or when an error occurs.
/// - Returns: A query watcher object that can be used to control the watching behavior.
func watch<Query: GraphQLQuery>(query: Query,
cachePolicy: CachePolicy,
callbackQueue: DispatchQueue,
resultHandler: @escaping GraphQLResultHandler<Query.Data>) -> GraphQLQueryWatcher<Query>

/// Performs a mutation by sending it to the server.
///
/// - Parameters:
/// - mutation: The mutation to perform.
/// - publishResultToStore: If `true`, this will publish the result returned from the operation to the cache store. Default is `true`.
/// - queue: A dispatch queue on which the result handler will be called. Defaults to the main queue.
/// - queue: A dispatch queue on which the result handler will be called. Should default to the main queue.
/// - resultHandler: An optional closure that is called when mutation results are available or when an error occurs.
/// - Returns: An object that can be used to cancel an in progress mutation.
func perform<Mutation: GraphQLMutation>(mutation: Mutation,
Expand All @@ -74,7 +76,7 @@ public protocol ApolloClientProtocol: class {
/// - Parameters:
/// - subscription: The subscription to subscribe to.
/// - fetchHTTPMethod: The HTTP Method to be used.
/// - queue: A dispatch queue on which the result handler will be called. Defaults to the main queue.
/// - queue: A dispatch queue on which the result handler will be called. Should default to the main queue.
/// - resultHandler: An optional closure that is called when mutation results are available or when an error occurs.
/// - Returns: An object that can be used to cancel an in progress subscription.
func subscribe<Subscription: GraphQLSubscription>(subscription: Subscription,
Expand Down
12 changes: 7 additions & 5 deletions Sources/Apollo/GraphQLQueryWatcher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ public final class GraphQLQueryWatcher<Query: GraphQLQuery>: Cancellable, Apollo
public let query: Query
let resultHandler: GraphQLResultHandler<Query.Data>

private let callbackQueue: DispatchQueue

private let contextIdentifier = UUID()

private var fetching: Atomic<Cancellable?> = Atomic(nil)
Expand All @@ -20,15 +22,18 @@ public final class GraphQLQueryWatcher<Query: GraphQLQuery>: Cancellable, Apollo
/// Designated initializer
///
/// - Parameters:
/// - client: The client protocol to pass in
/// - query: The query to watch
/// - client: The client protocol to pass in.
/// - query: The query to watch.
/// - callbackQueue: The queue for the result handler. Defaults to the main queue.
/// - resultHandler: The result handler to call with changes.
public init(client: ApolloClientProtocol,
query: Query,
callbackQueue: DispatchQueue = .main,
resultHandler: @escaping GraphQLResultHandler<Query.Data>) {
self.client = client
self.query = query
self.resultHandler = resultHandler
self.callbackQueue = callbackQueue

client.store.subscribe(self)
}
Expand All @@ -38,9 +43,6 @@ public final class GraphQLQueryWatcher<Query: GraphQLQuery>: Cancellable, Apollo
fetch(cachePolicy: .fetchIgnoringCacheData)
}

// Watchers always call result handlers on the main queue.
private let callbackQueue: DispatchQueue = .main

func fetch(cachePolicy: CachePolicy) {
fetching.mutate {
// Cancel anything already in flight before starting a new fetch
Expand Down