Search code examples
iosswiftreactive-programmingrx-swiftreactivex

RxSwift - Emit after not receiving items for X seconds


I have been trying to figure out a way to implement such behavior but was not able to come to a working solution.

What I want to achieve

var dataSource = Variable<[T]>([])

var arrayObserver: Observable<SomeType> {

    return dataSource.asObservable()
        .map({ (elementoftype[T]) in

        // do something with it and return element of SomeType
        })
}

The dataSource variable is fed with values coming in 1 by 1. I want the arrayObserver to be able to buffer / "hold" onto the mapping until it has been, say, 0.5 seconds since the dataSource last emitted.

Thanks a lot for your suggestions !


Solution

  • Turns out calling

    .throttle(0.5, MainScheduler.instance)
    

    before mapping does the job.