Last active
May 23, 2017 10:22
-
-
Save jeremiegirault/035300930701ca002b48cc9899db45f6 to your computer and use it in GitHub Desktop.
Ensure that the consumer of your api calls your callback
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
final class AssertComplete { | |
var defused: Bool = false | |
let function: String | |
init(function: String = #function) { | |
self.function = function | |
} | |
func defuse() { defused = true } | |
deinit { | |
assert(defused, "You must call complete when calling \(function)") | |
} | |
} | |
func delegateActionAndComplete(action: (() -> Void) -> Void) { | |
let assertComplete = AssertComplete() | |
action { | |
assertComplete.defuse() | |
// when complete | |
} | |
} | |
delegateActionAndComplete { complete in | |
complete() // do not forget to call complete or else, assert will trigger ! | |
} | |
print("done") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment