Reputation: 400
I'm trying to leverage combine when making some API calls and I'm at a loss for why the sink methods are not being called.
I've included what I believe are all the relevant parts of the code and also the output in the logs.
What should I be doing differently?
private var authorization: GTMAppAuthFetcherAuthorization?
private let service = GTLRGmailService()
private var cancellables = Set<AnyCancellable>()
...
private func executeQuery(query: GTLRGmailQuery) -> AnyPublisher<Any, Error> {
print("in executeQuery")
return Future<Any, Error> { promise in
self.service.executeQuery(query) {ticket, result, error in
if let error = error {
return promise(.failure(error))
} else {
print ("GOT ME SOME RESULTS")
print(result!)
return promise(.success(result!))
}
}
}.eraseToAnyPublisher()
}
func getThreads() {
print("in getThreads")
let query = GTLRGmailQuery_UsersThreadsList.query(
withUserId: authorization?.userEmail ?? "me"
)
print("getting threads")
self.executeQuery(query: query)
.sink(
receiveCompletion: { completion in
print("GOT COMPLETION")
print(completion)
switch completion {
case .failure(let error):
print("ERROR EXECUTING QUERY")
print(error)
case .finished:
break
}
},
receiveValue: { value in
print("VALUE FROM QUERY")
print(value)
}
)
.store(in: &cancellables)
}
Log output:
in getThreads
getting threads
in executeQuery
2022-01-15 22:27:57.875615-0500 mail[30582:6728076] GTMSessionFetcher invoking fetch callbacks, data {length = 32719, bytes = 0x7b227468 72656164 73223a5b 7b226964 ... 7465223a 3533387d }, error (null)
2022-01-15 22:27:57.878231-0500 mail[30582:6728134] Executing GTLRGmailQuery_UsersThreadsList has additional pages of results not fetched because shouldFetchNextPages is not enabled
GOT ME SOME RESULTS
GTLRGmail_ListThreadsResponse 0x600000c4fc30: {nextPageToken:"..." resultSizeEstimate:538 threads:[100]}
Upvotes: 2
Views: 2086
Reputation: 400
I read this and added a .print("debugging")
before .sink
and the logs now include the following:
appeared and getting threads
in getThreads
getting threads
in executeQuery
debugging: receive subscription: (Future)
debugging: request unlimited
debugging: receive cancel
What this made me realize is that before my sink
methods could be called the Future was being cancelled. So, I looked at where I was instantiating this class and realized that it was in a short-lived method (onAppear
of a view). I moved instantiation to somewhere longer-lived and that allowed for the operations to complete and for the sink
methods to be called.
Upvotes: 2