Skip to content

Instantly share code, notes, and snippets.

@r-plus
Last active August 14, 2017 14:27
Show Gist options
  • Save r-plus/fa63a3052e4b19dc4ee5d4b611da7d11 to your computer and use it in GitHub Desktop.
Save r-plus/fa63a3052e4b19dc4ee5d4b611da7d11 to your computer and use it in GitHub Desktop.
Migration to RxSwift from Promise: reduce+then編

PromiseからRxSwiftへの移行reduce+then編

Promise

[Promise]をreduceとthenで全部つなげる使い方

let promises: [Promise<Void>] = promises()
promises.reduce(Promise<Void>.resolve()) { (acc, val) In
    return acc.then(val)
}.then { _ in
    print("finish all promises")
}

RxSwift

concatで前のObservableのcompletedを待ってシリアルに実行。そのままsubscribeするとonNextの度にsubscribeまで来るのでtoArrayでまとめる。

        var a = [Observable<Int>]()
        for i in 1...10 {
            let obs = Observable<Int>.create({ (observer) -> Disposable in
                print("start task \(i)")
                DispatchQueue.global().asyncAfter(deadline: .now() + Double(i)) {
                    observer.onNext(i)
                    observer.onCompleted()
                }
                print("end task \(i)")
                return Disposables.create()
            }).debug()
            a.append(obs)
        }
        
        Observable.concat(a)
            .toArray()
            .subscribe(onNext: { (v) in
                print("finish \(v)")
            })
            .disposed(by: disposeBag)
2017-08-14 22:43:42.738: Playground.xcplaygroundpage:131 (reduce()) -> subscribed
start task 1
end task 1
2017-08-14 22:43:43.837: Playground.xcplaygroundpage:131 (reduce()) -> Event next(1)
2017-08-14 22:43:43.838: Playground.xcplaygroundpage:131 (reduce()) -> Event completed
2017-08-14 22:43:43.839: Playground.xcplaygroundpage:131 (reduce()) -> isDisposed
2017-08-14 22:43:43.839: Playground.xcplaygroundpage:131 (reduce()) -> subscribed
start task 2
end task 2
2017-08-14 22:43:46.039: Playground.xcplaygroundpage:131 (reduce()) -> Event next(2)
2017-08-14 22:43:46.040: Playground.xcplaygroundpage:131 (reduce()) -> Event completed
2017-08-14 22:43:46.040: Playground.xcplaygroundpage:131 (reduce()) -> isDisposed
2017-08-14 22:43:46.040: Playground.xcplaygroundpage:131 (reduce()) -> subscribed
start task 3
end task 3
2017-08-14 22:43:49.341: Playground.xcplaygroundpage:131 (reduce()) -> Event next(3)
2017-08-14 22:43:49.342: Playground.xcplaygroundpage:131 (reduce()) -> Event completed
2017-08-14 22:43:49.342: Playground.xcplaygroundpage:131 (reduce()) -> isDisposed
2017-08-14 22:43:49.342: Playground.xcplaygroundpage:131 (reduce()) -> subscribed
start task 4
end task 4
2017-08-14 22:43:53.343: Playground.xcplaygroundpage:131 (reduce()) -> Event next(4)
2017-08-14 22:43:53.344: Playground.xcplaygroundpage:131 (reduce()) -> Event completed
2017-08-14 22:43:53.344: Playground.xcplaygroundpage:131 (reduce()) -> isDisposed
2017-08-14 22:43:53.344: Playground.xcplaygroundpage:131 (reduce()) -> subscribed
start task 5
end task 5
2017-08-14 22:43:58.590: Playground.xcplaygroundpage:131 (reduce()) -> Event next(5)
2017-08-14 22:43:58.590: Playground.xcplaygroundpage:131 (reduce()) -> Event completed
2017-08-14 22:43:58.590: Playground.xcplaygroundpage:131 (reduce()) -> isDisposed
2017-08-14 22:43:58.590: Playground.xcplaygroundpage:131 (reduce()) -> subscribed
start task 6
end task 6
2017-08-14 22:44:05.191: Playground.xcplaygroundpage:131 (reduce()) -> Event next(6)
2017-08-14 22:44:05.191: Playground.xcplaygroundpage:131 (reduce()) -> Event completed
2017-08-14 22:44:05.192: Playground.xcplaygroundpage:131 (reduce()) -> isDisposed
2017-08-14 22:44:05.192: Playground.xcplaygroundpage:131 (reduce()) -> subscribed
start task 7
end task 7
2017-08-14 22:44:12.193: Playground.xcplaygroundpage:131 (reduce()) -> Event next(7)
2017-08-14 22:44:12.194: Playground.xcplaygroundpage:131 (reduce()) -> Event completed
2017-08-14 22:44:12.194: Playground.xcplaygroundpage:131 (reduce()) -> isDisposed
2017-08-14 22:44:12.194: Playground.xcplaygroundpage:131 (reduce()) -> subscribed
start task 8
end task 8
2017-08-14 22:44:20.993: Playground.xcplaygroundpage:131 (reduce()) -> Event next(8)
2017-08-14 22:44:20.994: Playground.xcplaygroundpage:131 (reduce()) -> Event completed
2017-08-14 22:44:20.994: Playground.xcplaygroundpage:131 (reduce()) -> isDisposed
2017-08-14 22:44:20.995: Playground.xcplaygroundpage:131 (reduce()) -> subscribed
start task 9
end task 9
2017-08-14 22:44:30.042: Playground.xcplaygroundpage:131 (reduce()) -> Event next(9)
2017-08-14 22:44:30.043: Playground.xcplaygroundpage:131 (reduce()) -> Event completed
2017-08-14 22:44:30.043: Playground.xcplaygroundpage:131 (reduce()) -> isDisposed
2017-08-14 22:44:30.043: Playground.xcplaygroundpage:131 (reduce()) -> subscribed
start task 10
end task 10
2017-08-14 22:44:41.025: Playground.xcplaygroundpage:131 (reduce()) -> Event next(10)
2017-08-14 22:44:41.026: Playground.xcplaygroundpage:131 (reduce()) -> Event completed
finish [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
2017-08-14 22:44:41.027: Playground.xcplaygroundpage:131 (reduce()) -> isDisposed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment