Longshihua
Longshihua

Reputation: 425

What's the difference between throttle and debounce in Rxswift3.0?

I have seen a lot of blogs about throttle and debounce. Most of them said they are the same thing. But I get the different result form my example? Here is the example:

let disposeBag = DisposeBag()
Observable.of(1,2,3,4,5)
          .debounce(1, scheduler: MainScheduler.instance)
          .subscribe(onNext: {print($0)})
          .addDisposableTo(disposeBag)

the result was 5. But when I used throttle, the result was 1

let disposeBag = DisposeBag()
Observable.of(1,2,3,4,5)
        .throttle(1, scheduler: MainScheduler.instance)
        .subscribe(onNext: {print($0)})
        .addDisposableTo(disposeBag)

So,I can't understand about the throttle operator?

Upvotes: 14

Views: 14576

Answers (2)

Shashank Mishra
Shashank Mishra

Reputation: 1079

Simple explanation is here https://medium.com/fantageek/throttle-vs-debounce-in-rxswift-86f8b303d5d4

Throttle: the original function is called at most once per specified period.

Debounce: the original function is called after the caller stops calling the decorated function after a specified period.

Upvotes: 2

Paulw11
Paulw11

Reputation: 114856

In earlier versions of RxSwift, throttle and debounce did the same thing, which is why you will see articles stating this. In RxSwift 3.0 they do a similar but opposite thing.

Both debounce and throttle are used to filter items emitted by an observable over time.

  • throttle emits only the first item emitted by the source observable in the time window.

  • debounce only emits an item after the specified time period has passed without another item being emitted by the source observable.

Both can be used to reduce the number of items emitted by an observable; which one you use depends on whether you want the "first" or "last" value emitted in a time period.

The term "debounce" comes from electronics and refers to the tendency of switch contacts to "bounce" between on and off very quickly when a switching action occurs. You won't notice this when you turn on a lightbulb but a microprocessor looking at an input thousands of times a second will see a rapid sequence of "ons" and "offs" before the switch settles into its final state. This is why debounce gives you the value of 5; the final item that was emitted in your time frame (1 ms). If you put a time delay into your code so that the items were emitted more slowly (more than 1ms apart) you would see a number of items emitted by debounce.

In an app you could use debounce for performing a search that is expensive (say it requires a network operation). The user is going to type a number of characters characters for their search string but you don't want to initiate a search as they enter each character, since the search is expensive and the earlier results will be obsolete by the time they return. Using debounce you can ensure that the search string is only emitted once the user stops typing for a period (say 500ms).

You might use throttle where an operation takes some time and you want to ignore further input until that time has elapsed. Say you have a button that initiates an operation. If the user taps the button multiple times in quick succession you only want to initiate the operation once. You can use throttle to ignore the subsequent taps within a specified time window. debounce could also work but would introduce a delay before the action item was emitted, while throttle allows you to react to the first action and ignore the rest.

Upvotes: 45

Related Questions