简体   繁体   中英

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 map ping until it has been, say, 0.5 seconds since the dataSource last emitted.

Thanks a lot for your suggestions !

Turns out calling

.throttle(0.5, MainScheduler.instance)

before mapping does the job.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM