2
func signup(_ username: String, password: String) -> Observable<Bool> {
    // this is also just a mock
    let signupResult = arc4random() % 5 == 0 ? false : true
    return Observable.just(signupResult)
        .concat(Observable.never())
        .throttle(0.4, scheduler: MainScheduler.instance)
        .take(1)
 }

I see some code in RxSwift, But I could not understand why need to concat(Observable.never())

3 Answers 3

1

I'm making sense of this myself, so take it with a grain of salt :)

  • Observable.just emits the value once and completes immediately; it takes virtually no time at all;
  • .throttle(0.4, scheduler: MainScheduler.instance) introduces time into the sequence, taking 0.4s+ to determine the value to return, and
  • concat(Observable.never()) makes the input sequence long-running so throttle has something to do in the first place.

Now return Observable.just(signupResult) would suffice to implement that function. What's the rest good for?

The other three operators result in a delay. Since the concatenated Observable is never-ending, throttle will wait at least 0.4 seconds once to determine which value to emit for the first throttled "chunk". take(1) then completes the sequence once the first value is emitted after 0.4s

To me, this seems to be a very laborious way to implement a delayed response :)

0

This is just example. FUI, never create an Observable that emits no items and does not terminate (description from official documentation)

1
  • Thank you,I know the usage of never,so I don`t understand why use never(),so this is just example~
    – 常小波
    Nov 28, 2016 at 2:51
0

The answer from @ctietze seems right. But after make a test, I realize that throttle kills the whole concept. Basically throttle will return the value right after the observable start. That mean it won't wait until 0.4s to emit the first value.

func testWithThrottle() -> Observable<Int> {
        return
            Observable.just(7) // 1
                .concat(Observable.never()) // 2
                .throttle(3.0, scheduler: Schedulers.background) // 3
                .take(1) // 4

        // 1. -7--|->
        // 2. -7------------------------------->
        // 3. -7------------------------------->
        // 4. -7--|->
}

...

print("[\(Date())] Start!")
testWithThrottle().subscribe({ (event) in
    print("[\(Date())] event: \(event)")
}).addDisposableTo(disposeBag)
print("[\(Date())] End!")

...

[2017-06-03 03:14:00 +0000] Start!
[2017-06-03 03:14:00 +0000] event: next(7)
[2017-06-03 03:14:00 +0000] event: completed
[2017-06-03 03:14:00 +0000] End!

SO, what is the solution? Easy, you need to use debounce instead.

func testWithDebounce() -> Observable<String> {
        return
            Observable.of("A") // 1
                .concat(Observable.never()) // 2
                .debounce(3.0, scheduler: Schedulers.background) // 3
                .take(1) // 4

        // 1. -A-|->
        // 2. -A------------------------------>
        // 3. ---------------A---------------->
        // 4. ---------------A-|->
}

...

[2017-06-03 03:24:21 +0000] Start Thrrotle!
[2017-06-03 03:24:21 +0000] Finish Thrrotle!
[2017-06-03 03:24:24 +0000] event: next(A)
[2017-06-03 03:24:24 +0000] event: completed

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.