David Collado
David Collado

Reputation: 358

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:

Any idea?

Upvotes: 0

Views: 57

Answers (1)

ajjnix
ajjnix

Reputation: 462

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

Upvotes: 1

Related Questions