I want to call a method which will return a value from its completion handler. The method performs asynchronously and I don\'t want to return a value before all the body of
You need to change getCurrentClient
to take in a completion block instead of returning a value.
For example:
-(void)getCurrentClientWithCompletionHandler:(void (^)(NSDictionary* currentClient))handler
{
NXOAuth2Account *currentAccount = [[[NXOAuth2AccountStore sharedStore] accounts] lastObject];
[NXOAuth2Request performMethod:@"GET"
onResource:[NSURL URLWithString:[NSString stringWithFormat:@"%@/clients/%@", kCatapultHost, currentAccount.userData[@"account_name"]]]
usingParameters:nil
withAccount:currentAccount
sendProgressHandler:nil
responseHandler:^ (NSURLResponse *response, NSData *responseData, NSError *error) {
NSError *jsonError;
NSDictionary* deserializedDict = [NSJSONSerialization JSONObjectWithData:responseData
options:kNilOptions
error:&jsonError];
handler(deserializedDict);
}];
}
It's important to remember that getCurrentClient
will return immediately, while the network request is dispatched on another thread. Don't forget that if you want to update the UI using your response handler, you need to have your handler run on the main thread.