How do I handle errors? I can use catch which then gives me the opportunity to then ask the delegate for a default location.
You're so close. Use +[RACSignal return:]
to create a signal which just sends your default location:
RACSignal *locationSignal = [fetchLocationSignal catch:^RACSignal *(NSError *error) {
NSLog(@"Unable to fetch location, going to grab default instead: %@", error);
CLLocation *defaultLocation = [lmDelegate defaultLocation];
NSLog(@"defaultLocation = [%@]", defaultLocation);
return [RACSignal return:defaultLocation];
}];
When I return the location, should it be done as sendNext or sendCompleted? Currently it's coded as sendNext, but it seems like something that would be done as sendCompleted.
In order to actually send data, you'll need to use -sendNext:
. If that's the only thing that the signal is sending, then it should also -sendCompleted
after that. But if it's going to continue to send the location as accuracy improves or location changes, then it shouldn't complete yet.
More generally, signals can send any numbers of next
s (0*) but can only complete or error. Once completion or error is sent, the signal's done. It won't send any more values.
Actually, does the answer to that depend on how I create the Delegate. This test app creates a new Delegate each time the view is loaded. Is that something I should do, I should I make the Delegate a singleton. If it's a singleton, then sendNext seems the right thing to do. But if would then imply that I move the code that I have in latestLocationSignal in my delegate into an init method instead?
I'm not sure I have enough context to answer this, except to say that singletons are never the answer ;) Creating a new delegate each time the view is loaded seems reasonable to me.
It's hard to answer broad questions like this because I don't know your design nearly as well as you do. Hopefully these answers help a bit. If you need more clarification, specific examples are really helpful.