The accepted answer will work around the problem, but there's a more idiomatic, elegant way to handle this:
First off, instead of using -doNext:
, use -map:
to transform your JSON into the array of users:
RAC(self, users) = [[self fetchRandomUserData] map:^(NSDictionary *json){
NSMutableArray *randomUsers = [NSMutableArray array];
for (NSDictionary *dict in json[@"data"]) {
BKRandomUser *randomUser = [MTLJSONAdapter modelOfClass:[BKRandomUser class] fromJSONDictionary:dict error:nil];
[randomUsers addObject:randomUser];
}
return randomUsers;
}];
Then, to handle the error, you can use -catch:
:
RAC(self, users) = [[[self fetchRandomUserData] map:^(NSDictionary *json){
NSMutableArray *randomUsers = [NSMutableArray array];
for (NSDictionary *dict in json[@"data"]) {
BKRandomUser *randomUser = [MTLJSONAdapter modelOfClass:[BKRandomUser class] fromJSONDictionary:dict error:nil];
[randomUsers addObject:randomUser];
}
return randomUsers;
}] catch:^(NSError *error) {
return [RACSignal return:@[]];
}];
In this example, if an error happens we catch it and replace it with an empty array. You could do whatever you wanted there. Replace it with nil
, or +[RACSignal empty]
if you just want to ignore the whole thing. Or call another method that returns a RACSignal *
.