Block successful failures from AFNetworking as part of a method

I need to encapsulate the response from AFNetworking calls in my own method when I write the library. This code closed me:

MyDevice *devices = [[MyDevice alloc] init]; [devices getDevices:@"devices.json?user_id=10" success:^(AFHTTPRequestOperation *operation, id responseObject) { ... can process json object here ... } - (void)getDevices:(NSString *)netPath success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error)) failure { [[MyApiClient sharedDeviceServiceInstance] getPath:[NSString stringWithFormat:@"%@", netPath] parameters:nil success:success failure:failure]; } 

However, I need to process the json object data returned from getPath before returning to getDevices (). I tried this:

 - (void)getDevices:(NSString *)netPath success:(void (^)(id myResults))success failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error)) failure { [[MyApiClient sharedDeviceServiceInstance] getPath:[NSString stringWithFormat:@"%@", netPath] parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) { ... can process json object here ... } failure:^(AFHTTPRequestOperation *operation, NSError *error) { ... process errors here ... }]; } 

But now there is no return to getDevices (). So, how can I process the json object in getDevices and return the block after completion? Please rate the help as I am new to blocks.

+7
source share
1 answer

This is very easy to do: just call the block by calling it as a function.

 - (void)getDevices:(NSString *)netPath success:(void (^)(id myResults))success failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error)) failure { [[MyApiClient sharedDeviceServiceInstance] getPath:netPath parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) { id myResults = nil; // ... can process json object here ... success(myResults); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { // ... process errors here ... failure(operation, error); }]; } 

EDIT:

Based on the code you posted, I think the following interface will be more understandable:

 typedef void(^tFailureBlock)(NSError *error); - (void)getDevicesForUserID:(NSString *)userID success:(void (^)(NSArray* devices))successBlock failure:(tFailureBlock)failureBlock; 
+10
source