I have the following code defined in a view controller.
_ = accountService.getAccount()
.then { user -> Void in
self.txtBxUser.text = user.username
self.txtBxEmail.text = user.email
}
getAccount
makes a REST API request.
If the user dismisses the view controller before the call has returned, what happens to the call back chain? Does it still run given that, I presume, it's still referenced?
If the user dismisses the view controller before the call has returned, what happens to the call back chain? Does it still run given that, I presume, it's still referenced?
Yes, it does still run.
Be forewarned that the reference to self
in the closure means that it is also keeping a reference to the view controller until that then
closure finishes running. For that reason, if there's a chance the view controller could have been dismissed, you might want to use a weak
reference:
_ = accountService.getAccount().then { [weak self] user -> Void in
self?.txtBxUser.text = user.username
self?.txtBxEmail.text = user.email
}
Ideally, you should also make getAccount
cancellable and cancel it in the view controller's deinit
.
(Note, in FAQ - Should I be Concerned About Retain Cycles, the PromiseKit documentation points out that you don't need weak
references, which is correct. It's just a question of whether or not you mind if the deallocation of the dismissed view controller is deferred until after the promise is fulfilled.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With