So I having a trouble when reading the Convert incoming raw data to your types with Combine operators section of Processing URL session data task results with Combine article.
Here is the template from the section, and I couldn't find where the cancellable
and urlSession
variable declared or their purpose.
struct User: Codable {
let name: String
let userID: String
}
let url = URL(string: "https://example.com/endpoint")!
//Where are these two variables coming from and going?
cancellable = urlSession
.dataTaskPublisher(for: url)
.tryMap() { element -> Data in
guard let httpResponse = element.response as? HTTPURLResponse,
httpResponse.statusCode == 200 else {
throw URLError(.badServerResponse)
}
return element.data
}
.decode(type: User.self, decoder: JSONDecoder())
.sink(receiveCompletion: { print ("Received completion: \($0).") },
receiveValue: { user in print ("Received user: \(user).")})
Retry transient errors and catch and replace persistent errors
The AnyCancellable
returned by sink
is an object on which you can call cancel()
should you need to stop the asynchronous work. But, as others have noted, the documentation tells us, it is also the case that, “An AnyCancellable
instance automatically calls cancel()
when deinitialized.” So, we need to store that AnyCancellable
instance in some ivar (either a simple AnyCancellable
or a Set<AnyCancellable>
property), not a local variable. If stored in a local variable, it will immediately fall out of scope and be canceled before the asynchronous work is performed.
In short, once we store it in that property, it will conveniently stop the work when that parent object falls out of scope. But this must be a property, not just a local variable of the function.