Search code examples
iosswiftrx-swiftreactivex

RxSwift: Receive events immediately, unless the last event was processed within a certain interval


New to RxSwift / Reactivex. Basically what I'm trying to do is to make a server call whenever something happens, but make sure it's not done more often than every 10 seconds. Less often if possible.

For instance, whenever an event ("needs update") is generated I'd like to call the server immediately if more than 10 seconds have passed since my last call. If less time has passed I'd like to make the call on the 10 second mark from the last one. It doesn't matter how many events have been generated within these 10 seconds.

I looked at the description of throttle but it appears to starve if events happen very quickly, which isn't desirable.

How can I achieve this?


Solution

  • There's a proposed new operator for RxSwiftExt that would give you something you're looking for, I think. However, it doesn't exist yet. You might want to keep an eye on it, though.

    https://github.com/RxSwiftCommunity/RxSwiftExt/issues/10