Rxswift: Disordered sequences when calling async function

Created on 19 Oct 2016  路  2Comments  路  Source: ReactiveX/RxSwift

Short description of the issue:

On the RxSwift 2.6.0, sequences will be disordered after calling async function inside flatmap.

Self contained code example that reproduces the issue:

func test() {
        [1, 2, 3, 4, 5, 6, 7, 8, 9, 10].toObservable()
            .flatMap { n -> Observable<Int> in
                print("START \(n)")
                return Observable.just(n)
        }
            .flatMap({ [weak self] n -> Observable<Int> in
                guard let sSelf = self else {
                    return Observable.never()
                }
                return sSelf.coba(n)
        }).flatMap({ n -> Observable<Int> in
                print("END \(n)")
                return Observable.just(n)
        }).subscribeNext({ n in
                print("NEXT \(n)")
        })
            .addDisposableTo(disposeBag)
}

func coba(n: Int) -> Observable<Int> {
        return Observable.create({ observer -> Disposable in
            //In my case, I call external function that calling `dispatch_async`
            let queue = dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0)
            dispatch_async(queue) {
                sleep(3)
                observer.onNext(n)
                observer.onCompleted()
            }

            return NopDisposable.instance
        })
}

Xcode version:

  7.3.1

Expected outcome:

START 1
END 1
NEXT 1
START 2
END 2
NEXT 2
START 3
END 3
NEXT 3
START 4
END 4
NEXT 4
START 5
END 5
NEXT 5
START 6
END 6
NEXT 6
START 7
END 7
NEXT 7
START 8
END 8
NEXT 8
START 9
END 9
NEXT 9
START 10
END 10
NEXT 10

What actually happens:

START 1
START 2
START 3
START 4
START 5
START 6
START 7
START 8
START 9
START 10
END 1
NEXT 1
END 2
NEXT 2
END 3
NEXT 3
END 4
NEXT 4
END 5
NEXT 5
END 7
NEXT 7
END 8
NEXT 8
END 6
NEXT 6
END 9
NEXT 9
END 10
NEXT 10

Installation method:

  • [x] CocoaPods
  • [ ] Carthage
  • [ ] Git submodules

I have multiple versions of Xcode installed:
(so we can know if this is a potential cause of your issue)

  • [x] yes (xcode 8)
  • [ ] no

Level of RxSwift knowledge:
(this is so we can understand your level of knowledge
and formulate the response in an appropriate manner)

  • [ ] just starting
  • [x] I have a small code base
  • [ ] I have a significant code base

Most helpful comment

@rikoap As far as I can tell flatMap does not guarantee order of events from mapped observables
What you probably what to use is map { .. } .concat()

All 2 comments

@rikoap As far as I can tell flatMap does not guarantee order of events from mapped observables
What you probably what to use is map { .. } .concat()

Thanks @sergdort , it's solve my issue.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Z-JaDe picture Z-JaDe  路  3Comments

gaudecker picture gaudecker  路  3Comments

RafaelPlantard picture RafaelPlantard  路  3Comments

hannesstruss picture hannesstruss  路  3Comments

acecilia picture acecilia  路  3Comments