Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Confusion about flatMapLatest in RxSwift

I learn the sample code in RxSwift. In the file GithubSignupViewModel1.swift, the definition of validatedUsername is:

validatedUsername = input.username //the username is a textfiled.rx_text
    .flatMapLatest { username -> Observable<ValidationResult> in
        print("-------->1:")
        return validationService.validateUsername(username)
            .observeOn(MainScheduler.instance)
            .catchErrorJustReturn(.Failed(message: "Error contacting server"))
    }
    .shareReplay(1)

the validateUsername method is finally called the following method:

func usernameAvailable(username: String) -> Observable<Bool> {
    // this is ofc just mock, but good enough
    print("-------->2:")
    let URL = NSURL(string: "https://github.com/\(username.URLEscaped)")!
    let request = NSURLRequest(URL: URL)
    return self.URLSession.rx_response(request)
        .map { (maybeData, response) in
            print("-------->3:")
            return response.statusCode == 404
        }
        .catchErrorJustReturn(false)
}

Here is my confusion:

whenever I input a character quickly in the username textfield, message -------->1:, -------->2: showed, and a little later message -------->3: showed, but only showed one -------->3: message.

When I input characters slower, message -------->1:, -------->2:, -------->3: showed successively.

But when I change the flatMapLatest to flatMap, how many characters I input, I will get the same number of -------->3: message.

So how did the flatMapLatest work here?

How the flatMapLatest filter the early response from NSURLResponse ?


I read some about the flatMapLatest, but none of them will explain my confusion.

What I saw is something like:

let a = Variable(XX)
a.asObservable().flatMapLatest(...)

When changed a.value to another Variable, the Variable(XX) will not influence the subscriber of a.

But the input.username isn't changed, it is always a testfield.rx_text! So how the flatMapLatest work?

like image 841
leizh00701 Avatar asked May 26 '16 08:05

leizh00701


4 Answers

TheDroidsOnDroid's answer is clear for me:

FlatMapLatest diagram

Well, flatMap() gets one value, then performs long task, and when it gets the next value, previous task will still finish even when the new value arrives in the middle of the current task. It isn’t really what we need because when we get a new text in the search bar, we want to cancel the previous request and start another. That’s what flatMapLatest() does.

http://www.thedroidsonroids.com/blog/ios/rxswift-examples-3-networking/

You can use RxMarbles app on Appstore to play around with operators.

like image 170
dangthaison.91 Avatar answered Oct 10 '22 10:10

dangthaison.91


It's not clear what your confusion is about. Are you questioning the difference between flatMap and flatMapLatest? flatMap will map to a new Observable, and if it needs to flatMap again, it will in essence merge the two mapped Observables into one. If it needs to flatMap again, it will merge it again, etc.

With flatMapLatest, when a new Observable is mapped, it overwrites the last Observable if there was one. There is no merge.

EDIT: In response to your comment, the reason you aren't seeing any "------>3:" print is because those rx_request Observables were disposed before they could compete, because flatMapLatest received a new element, and this mapped to a new Observable. Upon disposal, rx_request probably cancels the request and will not run the callback where you're printing. The old Observable is disposed because it no longer belongs to anyone when the new one takes its place.

like image 26
solidcell Avatar answered Oct 10 '22 09:10

solidcell


I find this https://github.com/ReactiveX/RxSwift/blob/master/Rx.playground/Pages/Transforming_Operators.xcplaygroundpage/Contents.swift to be useful

Transforms the elements emitted by an Observable sequence into Observable sequences, and merges the emissions from both Observable sequences into a single Observable sequence. This is also useful when, for example, when you have an Observable sequence that itself emits Observable sequences, and you want to be able to react to new emissions from either Observable sequence. The difference between flatMap and flatMapLatest is, flatMapLatest will only emit elements from the most recent inner Observable sequence.

    let disposeBag = DisposeBag()

    struct Player {
        var score: Variable<Int>
    }

    let πŸ‘¦πŸ» = Player(score: Variable(80))
    let πŸ‘§πŸΌ = Player(score: Variable(90))

    let player = Variable(πŸ‘¦πŸ»)

    player.asObservable()
        .flatMap { $0.score.asObservable() } // Change flatMap to flatMapLatest and observe change in printed output
        .subscribe(onNext: { print($0) })
        .disposed(by: disposeBag)

    πŸ‘¦πŸ».score.value = 85

    player.value = πŸ‘§πŸΌ

    πŸ‘¦πŸ».score.value = 95 // Will be printed when using flatMap, but will not be printed when using flatMapLatest

    πŸ‘§πŸΌ.score.value = 100

With flatMap, we get

80
85
90
95
100

With flatMapLatest, we get

80
85
90
100

In this example, using flatMap may have unintended consequences. After assigning πŸ‘§πŸΌ to player.value, πŸ‘§πŸΌ.score will begin to emit elements, but the previous inner Observable sequence (πŸ‘¦πŸ».score) will also still emit elements. By changing flatMap to flatMapLatest, only the most recent inner Observable sequence (πŸ‘§πŸΌ.score) will emit elements, i.e., setting πŸ‘¦πŸ».score.value to 95 has no effect.

flatMapLatest is actually a combination of the map and switchLatest operators.

Also, I find https://www.raywenderlich.com/158205/rxswift-transforming-operators this to be useful

flatMap

keeps up with each and every observable it creates, one for each element added onto the source observable

flatMapLatest

What makes flatMapLatest different is that it will automatically switch to the latest observable and unsubscribe from the the previous one.

like image 18
onmyway133 Avatar answered Oct 10 '22 08:10

onmyway133


I think this diagram from Ray Wenderlich tutorial can help.

flatMapLatest diagram

like image 8
Nominalista Avatar answered Oct 10 '22 08:10

Nominalista