Reputation: 2488
I have a class, EventPresentationController, that displays data in an Event object. The class needs to be passed an Event object, but sometimes that object needs to be downloaded from a remote service first. I don't want the controller to have any knowledge of networking stuff, so I thought that instead of passing in an Event object, I'd pass it a RAC signal. Then if the object exists I can send the signal immediately, but if the object needs to be downloaded, I can download it then send the signal. However, I'd like for the controller to display an activity indicator if in fact a download needs to occur.
I see that RACCommand has an executing signal I can subscribe to, so I suppose I can pass that instead of an event, but semantically it seems weird to be initializing a view controller with a "command" (vs. an event, or a signal that will deliver an event). Is using RACCommand the right thing to do?
Upvotes: 1
Views: 745
Reputation: 2465
That's an interesting question. I think your instincts are right that you should pass the view controller a signal.
So let's go with that. Then let's suppose your view controller has a activityIndicator
property.
You should be able to do something like:
- (id)initWithEventSignal:(RACSignal *)signal {
// ... init stuff ...
// Send a YES and then a NO when `signal` completes. If `signal`
// immediately sends a value, then it will immediately send NO.
RACSignal *loading = [[[RACSignal return:@YES] takeUntil:signal] concat:[RACSignal return:@NO]];
RAC(self.activityIndicator, hidden) = [loading not];
RAC(self, event) = signal;
// ... other stuff ...
}
We're using -takeUntil:
to short-circuit our loading
signal when the event signal sends its result.
Upvotes: 1