Question

I'm writing an method which gets a Signal from a Webservice and then converts it from a NSDictionary to an object. But what if there's an error happening in this conversion?

Will I return an RACSignal error:error] then?

[signal map:^id(NSDictionary *dictionary) {
    NSError *error;
    SAMWebServiceResponse *samResponse = [MTLJSONAdapter modelOfClass: SAMWebServiceResponse.class
                                                   fromJSONDictionary: dictionary
                                                                error: &error];
    if (error) {
        //TODO: Don't know if this is the way to go.
        return [RACSignal error:error];
    } else {
        return samResponse;
    }
  }
]
Was it helpful?

Solution

ReactiveCocoa has a construct exactly for this situation called tryMap:. Check it out:

[signal tryMap:^id(NSDictionary *dictionary, NSError **errorPtr) {
    return [MTLJSONAdapter modelOfClass:SAMWebServiceResponse.class
                     fromJSONDictionary:dictionary
                                  error:errorPtr];
}]

This assumes that modelOfClass:fromJSONDictionary:error: will return nil when an error occurs -- which is pretty standard -- but it's worth checking the docs on that just in case. The code as you've written it now will just return that error signal, which is a completely legitimate thing to do (signals of signals are the best part of RAC), but not what you want here.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top