nacross
nacross

Reputation: 2043

How create a RACSequence from a method that returns an array

Given a method that returns an array of objects, how could you create a sequence that is only populated with the results from that method when it is used?

- (NSArray *) methodA { ... }

- (RACSequence *) methodB {
  return [self methodA].rac_sequence;
}

I am wondering is it possible to avoid execution of methodA unless the sequence is actually used but still return the sequence from methodB to pass on incase I decide to use it.

Update

I managed to achieve the behaviour I wanted by using a signal instead of sequence.

- (RACSignal *)methodB {
    RACSignal *racSignal = [RACSignal defer:^RACSignal * {
        return [self methodA].rac_sequence.signal;
    }];

    return racSignal;
}

Now methodA is only called when the signal is subscribed to. Why is there no similar concept for deferring sequences?

Upvotes: 3

Views: 373

Answers (1)

joshaber
joshaber

Reputation: 2465

No, since you start with an NSArray, it's already been evaluated by the time rac_sequence is called. If -methodA can return a RACSequence instead, the evaluation of the sequence would be delayed until it is needed.

Upvotes: 2

Related Questions