Skip to content

Commit 58e5dbe

Browse files
Merge pull request #854 from gsabran/gui--weak-ref-to-apollo-client
Use weak references to Apollo client in closures
2 parents a04b481 + c5ed443 commit 58e5dbe

1 file changed

Lines changed: 15 additions & 6 deletions

File tree

Sources/Apollo/ApolloClient.swift

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,10 @@ public class ApolloClient {
6464
}
6565

6666
fileprivate func send<Operation: GraphQLOperation>(operation: Operation, shouldPublishResultToStore: Bool, context: UnsafeMutableRawPointer?, resultHandler: @escaping GraphQLResultHandler<Operation.Data>) -> Cancellable {
67-
return networkTransport.send(operation: operation) { result in
67+
return networkTransport.send(operation: operation) { [weak self] result in
68+
guard let self = self else {
69+
return
70+
}
6871
self.handleOperationResult(shouldPublishResultToStore: shouldPublishResultToStore,
6972
context: context,
7073
result,
@@ -90,7 +93,10 @@ public class ApolloClient {
9093

9194
firstly {
9295
try response.parseResult(cacheKeyForObject: self.cacheKeyForObject)
93-
}.andThen { (result, records) in
96+
}.andThen { [weak self] (result, records) in
97+
guard let self = self else {
98+
return
99+
}
94100
if let records = records {
95101
self.store.publish(records: records, context: context).catch { error in
96102
preconditionFailure(String(describing: error))
@@ -175,7 +181,10 @@ extension ApolloClient: ApolloClientProtocol {
175181
return EmptyCancellable()
176182
}
177183

178-
return uploadingTransport.upload(operation: operation, files: files) { result in
184+
return uploadingTransport.upload(operation: operation, files: files) { [weak self] result in
185+
guard let self = self else {
186+
return
187+
}
179188
self.handleOperationResult(shouldPublishResultToStore: true,
180189
context: context, result,
181190
resultHandler: wrappedHandler)
@@ -206,7 +215,7 @@ private func wrapResultHandler<Data>(_ resultHandler: GraphQLResultHandler<Data>
206215
}
207216

208217
private final class FetchQueryOperation<Query: GraphQLQuery>: AsynchronousOperation, Cancellable {
209-
let client: ApolloClient
218+
weak var client: ApolloClient?
210219
let query: Query
211220
let cachePolicy: CachePolicy
212221
let context: UnsafeMutableRawPointer?
@@ -235,7 +244,7 @@ private final class FetchQueryOperation<Query: GraphQLQuery>: AsynchronousOperat
235244
return
236245
}
237246

238-
client.store.load(query: query) { result in
247+
client?.store.load(query: query) { result in
239248
if self.isCancelled {
240249
self.state = .finished
241250
return
@@ -262,7 +271,7 @@ private final class FetchQueryOperation<Query: GraphQLQuery>: AsynchronousOperat
262271
}
263272

264273
func fetchFromNetwork() {
265-
networkTask = client.send(operation: query, shouldPublishResultToStore: true, context: context) { result in
274+
networkTask = client?.send(operation: query, shouldPublishResultToStore: true, context: context) { result in
266275
self.resultHandler(result)
267276
self.state = .finished
268277
return

0 commit comments

Comments
 (0)