Mumthezir VP
Mumthezir VP

Reputation: 6551

Returning from a block - Objective C

I have a class method containing a block, of AFNetworking in which i want to return one dictionary variable, code shown below:

+(NSMutableDictionary*) getFromUrl:(NSString*)url parametersPassed:(NSDictionary*)parameters;
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    __block NSMutableDictionary *resultarray;
    
    [manager GET:url parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject)
     {
         NSLog(@"JSON: %@", responseObject);
         NSMutableDictionary *resultarrayTwo = (NSMutableDictionary *)responseObject;
         resultarray = resultarrayTwo;
     }
      failure:^(AFHTTPRequestOperation *operation, NSError *error)
    {
         NSLog(@"Error: %@, %@", error, operation.responseString);
         UIAlertView *alertView=[[UIAlertView alloc] initWithTitle:@"Message" message:@"Try again" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
         [alertView show];
     }];
    return resultArray;
}

How can i return resultArray here, it returns nothing here due to the difference in control flow.

I don't have much knowledge in Objective C block. Waiting for your help.

Thank you.

Upvotes: 1

Views: 147

Answers (3)

iphonic
iphonic

Reputation: 12717

Change your function design to the following function using Completion Blocks

+(void)getFromUrl:(NSString*)url parametersPassed:(NSDictionary*)parameters completion:(void (^) (NSMutableArray *values))completion;
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

    [manager GET:url parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject)
     {
         NSLog(@"JSON: %@", responseObject);
         NSMutableDictionary *resultarray = (NSMutableDictionary *)responseObject;
         completion(resultarray);
     }
      failure:^(AFHTTPRequestOperation *operation, NSError *error)
    {
         NSLog(@"Error: %@, %@", error, operation.responseString);
         UIAlertView *alertView=[[UIAlertView alloc] initWithTitle:@"Message" message:@"Try again" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
         [alertView show];
         completion(nil);
     }];

}

And call the function

[YourClass getFromUrl:url parametersPassed:params completion:^(NSMutableArray *values) {
        NSLog(@"%@",values);
 }];

Update Removed the extra array used.

Hope this helps.

Upvotes: 2

Sujit
Sujit

Reputation: 658

Before getting the response for GET call, it will execute the next lines. Thats why you are getting no data into Array.

You can call delegate method in success and failure block. This is the best solution.

Upvotes: 0

rounak
rounak

Reputation: 9397

The network call is asynchronous in nature, so having this method return its result isn't probably the right way of thinking as it implies you do a synchronous network call.

Have the method take a block parameter instead, and execute that block with the result at a later point of time from the AFNetworking completion block.

Upvotes: 1

Related Questions