I consume a RestApi with few Combine Publishers and want to visually represent ongoing progress with UIActivityIndicatorView. My current logic uses handleEvents->receiveSubscription
closure to start an animation and handleEvents->receiveCancel
and sink->completion
to stop an animation of the activityIndicatorView.
I ask for a review because it looks strange that a stop animation
must be called from two different closures which looks like I am missing something
let first = URLSession.shared.dataTaskPublisher(for: .init(string: "http://httpbin.org/delay/10")!)
.tryMap { (data: Data, response: URLResponse) in
return data
}
let second = URLSession.shared.dataTaskPublisher(for: .init(string: "http://httpbin.org/delay/5")!)
.tryMap { (data: Data, response: URLResponse) in
return data
}
cancellable = Publishers.Zip(first,second)
.flatMap { _ in
first
}
.handleEvents(receiveSubscription: { subscription in
print("receiveCancel: activityIndicatorView.startAnimating()")
}, receiveCancel: {
print("receiveCancel: activityIndicatorView.stopAnimating()")
})
.sink { _ in
print("sink completion: activityIndicatorView.stopAnimating()")
} receiveValue: { _ in }