Last active
March 23, 2018 15:56
-
-
Save olbrichj/118923f7284aac16fe3ee065dfb7bd10 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
extension Foo { | |
static func promise() -> Promise<FooResult> { | |
return PromiseWithDelegate().promise | |
} | |
} | |
class PromiseWithDelegate: FooDelegate { | |
let (promise, seal) = Promise<FooResult>.pending() | |
private let foo = Foo() | |
init() { | |
super.init() | |
retainCycle = self | |
foo.delegate = self // does not retain hence the `retainCycle` property | |
promise.ensure { | |
// ensure we break the retain cycle | |
self.retainCycle = nil | |
} | |
} | |
func fooSuccess(data: FooResult) { | |
seal.fulfill(data) | |
} | |
func fooError(error: FooError) { | |
seal.reject(error) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment