Observable subscribe alway once issue

Karim

I writing async unit tests for RxSwift,this my code,I can't understand subscribe only once

class TestViewModel: NSObject {
    let result : Observable<Int>

     init(input:Observable<Int>) {
        result = input.flatMapLatest({ (value) -> Observable<Int> in

            return Observable.create({ (observer) -> Disposable in

                DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1, execute: {
                print("next"+"  \(value)")
                    observer.onNext(value)
                })

                return Disposables.create()
            })
        })
    }

}

    func testCount() {
        let expectation = XCTestExpectation(description: "async")
        let input = scheduler.createHotObservable([.next(100, 1),.next(200, 10)])
        let viewModel = TestViewModel.init(input: input.asObservable())
        viewModel.result.subscribe(onNext: { (value) in
            print("subscribe"+"   \(value)")
        }).disposed(by: disposeBag)
        scheduler.start()
        wait(for: [expectation], timeout: timeout)
    }  

print info:

next  1
next  10
subscribe   10  

I think print info should :

next  1
next  10
subscribe   1  
subscribe   10    

Someone can give me suggestion?thank

Maxim Kosov

It's how flatMapLatest operator works. Its basically tells "map events into observables but use only recent observable's result". So, you map your events into two observables:

1: --1sec-> 1

10: --1sec-> 10

Most recent observable at the moment is for 10.

Try to use flatMap instead of flatMapLatest.

You should also avoid Observable.create if possible. In your particular case (to delay a value) you could use Observable.timer or Observable.just(...).delay(...).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

RxJS - subscribe only once but do not complete Observable

From Dev

RxJS Observable - subscribe once every time a condition met

From Dev

RxJS Observable - subscribe once every time a condition met

From Java

Observable Finally on Subscribe

From Dev

Subscribe Observable value

From Dev

Subscribe a Navigation Drawer to an Observable

From Dev

Subscribe subscriber to another observable?

From Dev

Subscribe Observable value

From Dev

Create observable, subscribe to it and return it

From Dev

Meteor - publish and subscribe issue

From Dev

How to subscribe to event emitter once?

From Dev

RxJava How to subscribe a Subject to an Observable

From Dev

How to subscribe to change of an observable field

From Dev

Subscribe on RxJava observable multiple times

From Dev

Subscribe to an Observable without triggering it and then passing it on

From Dev

RxJava How to subscribe a Subject to an Observable

From Dev

How to subscribe to change of an observable field

From Dev

Angular 2 Observable subscribe not working

From Dev

Subscribe to Observable returning wrong values

From Dev

CheckAll/UncheckAll issue with Subscribe ? Knockout

From Dev

subscribe to route to get observable and do something with it in one subscribe

From Dev

rxjs using promise only once on subscribe

From Dev

iOS, LiveQuery - Subscribe to a range of events at once?

From Java

Difference between the methods .pipe() and .subscribe() on a RXJS observable

From Dev

Re-subscribe to an observable with rx-angular

From Dev

Knockout.js: Subscribe or computed observable?

From Dev

Stop extend and subscribe on observable for a different page

From Dev

Subscribe to child component's Observable (valueChanges)

From Dev

Is it possible to re-subscribe to a Retrofit 2 observable?

Related Related

  1. 1

    RxJS - subscribe only once but do not complete Observable

  2. 2

    RxJS Observable - subscribe once every time a condition met

  3. 3

    RxJS Observable - subscribe once every time a condition met

  4. 4

    Observable Finally on Subscribe

  5. 5

    Subscribe Observable value

  6. 6

    Subscribe a Navigation Drawer to an Observable

  7. 7

    Subscribe subscriber to another observable?

  8. 8

    Subscribe Observable value

  9. 9

    Create observable, subscribe to it and return it

  10. 10

    Meteor - publish and subscribe issue

  11. 11

    How to subscribe to event emitter once?

  12. 12

    RxJava How to subscribe a Subject to an Observable

  13. 13

    How to subscribe to change of an observable field

  14. 14

    Subscribe on RxJava observable multiple times

  15. 15

    Subscribe to an Observable without triggering it and then passing it on

  16. 16

    RxJava How to subscribe a Subject to an Observable

  17. 17

    How to subscribe to change of an observable field

  18. 18

    Angular 2 Observable subscribe not working

  19. 19

    Subscribe to Observable returning wrong values

  20. 20

    CheckAll/UncheckAll issue with Subscribe ? Knockout

  21. 21

    subscribe to route to get observable and do something with it in one subscribe

  22. 22

    rxjs using promise only once on subscribe

  23. 23

    iOS, LiveQuery - Subscribe to a range of events at once?

  24. 24

    Difference between the methods .pipe() and .subscribe() on a RXJS observable

  25. 25

    Re-subscribe to an observable with rx-angular

  26. 26

    Knockout.js: Subscribe or computed observable?

  27. 27

    Stop extend and subscribe on observable for a different page

  28. 28

    Subscribe to child component's Observable (valueChanges)

  29. 29

    Is it possible to re-subscribe to a Retrofit 2 observable?

HotTag

Archive