0

I am running AFNetworking asynchronously. See code below.

I want to run it synchronously to do a package download (I need to get the list of services that people will be able to choose from). How can I do this?

Thanks.

 NSMutableURLRequest *apiRequest =
[self multipartFormRequestWithMethod:@"POST"
                                path: pathstr
                          parameters: params
           constructingBodyWithBlock: ^(id <AFMultipartFormData>formData)
           {
               //TODO: attach file if needed
           }];

AFJSONRequestOperation* operation = [[AFJSONRequestOperation alloc] initWithRequest: apiRequest];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
    //success!
    completionBlock(responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    //failure :(

    NSLog(@"%@", error);
    completionBlock([NSDictionary dictionaryWithObject:[error localizedDescription] forKey:@"error"]);
}];

[operation start];
Kermit the Frog
  • 3,949
  • 7
  • 31
  • 39
  • 1
    Check out this post by Mattt the creator of AFNetworking. http://stackoverflow.com/questions/7969865/can-afnetworking-return-data-synchronously-inside-a-block/7970037#7970037 – Keith Smiley Dec 08 '12 at 04:18

1 Answers1

1

I want to run it synchronously to do a package download (I need to get the list of services that people will be able to choose from). How can I do this?

AFNetworking does not support sync operations.

On the other hand, you almost never want sync operations on your UI thread, because the UI would become unresponsive.

(I need to get the list of services that people will be able to choose from)

What you could do is displaying some UI hint that the list of services is being downloaded while the async operation is in progress.

sergio
  • 68,819
  • 11
  • 102
  • 123