Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A cancellable completion for Swift.
final class Completion<R> {
private let closure: (R) -> Void
private var cancelled = false
/// `closure` is called upon completion, if not cancelled.
init(closure: (R) -> Void) {
self.closure = closure
}
/// `object` is held as a weak reference and passed to the closure upon completion, if not cancelled.
init<O:AnyObject>(object: O, closure: (O, R) -> Void) {
self.closure = { [weak object] r in
guard let strongObject = object else {
return
}
closure(strongObject, r)
}
}
func cancel() {
cancelled = true
}
func handler(result: R) {
if cancelled {
Logger.logVerbose("Completion handler called but doing nothing; cancelled.")
return
}
closure(result)
}
}
/* In use:
let completion: Completion<Result<String>>
completion = Completion(object: self) { me, result in // self is weakly held
me.receive(result)
}
webService.fetchStringWithCompletion(completion.handler)
completion.cancel() // block is never called
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.