0
-(NSNumber *)method:{

   NSString *url = urlToPost;
   NSNumber *success = [[NSNumber alloc]init];

   AFHTTPRequestOperationManagerObject POST:url parameters:@{} success:^(AFHTTPRequestOperation *operation, id responseObject) {

        success = [NSNumber numberWithBool:YES];

   } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

        success = [NSNumber numberWithBool:NO];

   }]; 

   return success;

}

Everytime this method is called success is returned as null. I have tried dispatching the block on the main queue as other similar StackOverflow answers suggest but the same problem persists.

Kerberos
  • 4,036
  • 3
  • 36
  • 55
rmp2150
  • 777
  • 1
  • 11
  • 22
  • check this link : http://stackoverflow.com/questions/7969865/can-afnetworking-return-data-synchronously-inside-a-block?lq=1 – V-Xtreme Jan 08 '15 at 04:02

1 Answers1

0

success is null because it is returned before getting assigned inside the block.
An asynchronous method runs the code in a background thread and calls the block at a later time. The rest of the function is executed without wait after the functionAFHTTPRequestOperationManagerObject POST:url parameters:@{} success:^ is called.
Thus the execution of the return success may take place before the completion block is called in the asynchronous function.

rakeshbs
  • 24,392
  • 7
  • 73
  • 63
  • i've tried dispatching the block on the mainthread but the same problem persists. is there anyway to make it wait for the block before returning? – rmp2150 Jan 08 '15 at 03:58
  • 1
    No, the function will complete asynchronously because the network activity will take some time - Event driven programming is an important concept in modern app development so you should learn how to work with it. In this case you need to invoke a method to process the success/fail from the block rather than relying on the return of the method. – Paulw11 Jan 08 '15 at 04:06