简体   繁体   中英

Transform signal of elements into arrays with size using ReactiveCocoa

I'm trying to find a way to transform a signal that sends X element into arrays of X elements limited by size.

Something like:

signal.take(2).collect().observeNext{changes in myFunction(changes) }

But that dies after completed. I need it to be:

  • Take 2 elements
  • Send array to function
  • Repeat

Any idea?

I solved this task (for location) and it's my solution

extention SignalProducer {
    func accumulate(size: Int) -> SignalProducer<[Value], Error> {
        var values: [Value] = []
        func next(value: Value) {
            if values.count >= size {
                values.removeAll()
            }
            values.append(value)
        }
        return on(next: next)
            .filter { _ in values.count < size }
            .map { _ -> [Value] in return values }
    }
}

https://github.com/ReactiveCocoa/ReactiveCocoa/pull/2817

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