Search code examples
iosreactive-cocoareactive-cocoa-3

ReactiveCocoa 3 - bufferWithTime


I've been bridging to use RACSignal.bufferWithTime. As far as I know there is no equivalence of it in RAC 3? Is there a workaround / trick to imitate the behaviour of bufferWithTime using RAC3?

My case scenario is that I need to track if the user tapped twice in a given short period (to capture double tap). I basically buffer it with about 0.2 seconds and see if there was more than a single tap. FIY, I cannot use UIGestureRecognizer since I use third party object which only exposes a single tap API.

Thanks!


Solution

  • You don't need to bufferWithTime, it can be done simply by checking timestamps on each tap. Follow these steps:

    1. Create a Signal of tap events on the third party object.
    2. map the Signal of tap events into timestamps representing the current time when the tap occurs.
    3. Use combinePrevious on the Signal of tap events to create a Signal whose values are a tuple of two timestamps: the first is previous value and the second is the current value.
    4. filter this signal by subtracting the first value in each tuple from the second value, and comparing whether it meets or exceeds your 0.2-second threshold.

    Whenever the final signal sends a value, a tap occurred within 0.2 seconds of the previous tap.