Swift 3: 将 PromiseKit deferred 转换为 RxSwift

Swift 3: Convert PromiseKit deferred to RxSwift

我目前正在用 RxSwift 替换 PromiseKit,并且需要将我的延迟承诺转换为 RxSwift。

PromiseKit 中的当前实现示例:

private var deferredDidLayout = Promise<()>.pending()

override func layoutSubviews() {
    super.layoutSubviews()

    self.deferredDidLayout.fulfill()
}

func setup() {
    _ = self.didLayout().then {_ -> Void in
        // Do my stuff only one time!
    }
}

private func didLayout() -> Promise<()> {
    return self.deferredDidLayout.promise
}

RxSwift 中当前的 hack-implementation:

private let observableDidLayout = PublishSubject<Void>()

override func layoutSubviews() {
    super.layoutSubviews()

    self.observableDidLayout.onCompleted()
}

func setup() {
     _ = self.observableDidLayout
        .subscribe(onCompleted: { _ in
            // Do my stuff only one time!
            // Issue: Will be executed on every onCompleted() call
     })
}

谢谢您的关注!

PromiseKit:https://github.com/mxcl/PromiseKit RxSwift:https://github.com/ReactiveX/RxSwift

我相信 'Completable' 就是您要找的 - https://github.com/ReactiveX/RxSwift/blob/master/Documentation/Traits.md#creating-a-completable