简体   繁体   中英

Objective-C GCD wait for Block to finish (AFNetworking)

I've been trying to experiment with some code from a tutorial, however not having much success due to not getting my head around GCD.

I have an class named API.m and here is the code regarding GCD:

+ (API *) sharedInstance
{
    static API *sharedInstance = nil;
    static dispatch_once_t oncePredicate;
    dispatch_once(&oncePredicate, ^{
        sharedInstance = [[self alloc] initWithBaseURL:[NSURL URLWithString:APIHost]];
    });

    return sharedInstance;
}

-(void)commandWithParams:(NSMutableDictionary*)params
            onCompletion:(JSONResponseBlock)completionBlock
{
    NSMutableURLRequest *apiRequest = [self multipartFormRequestWithMethod:@"POST"
                                                                      path:APIPath
                                                                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 :(
        completionBlock([NSDictionary dictionaryWithObject:[error localizedDescription] forKey:@"error"]);
    }];

    [operation start];
}

I make a simple test by implementing a button and getting an NSArray to print it's content to the output window:

- (IBAction)test:(id)sender {

    NSMutableDictionary* params =[NSMutableDictionary dictionaryWithObjectsAndKeys:
                                  @"pending", @"command",
                                  [[[API sharedInstance] user] objectForKey:@"UserID"] , @"userID",
                                  nil];

    [[API sharedInstance] commandWithParams:params
                               onCompletion:^(NSDictionary *json) {
                                   //result returned

                                   if ([json objectForKey:@"error"]==nil) {
                                       // Simple example
                                       [self.users addObject:@"1"];

                                   } else {
                                       //error
                                       [UIAlertView title:@"Error" withMessage:[json objectForKey:@"error"]];
                                   }

                               }];
    NSLog(@"%@", self.users);
}

Now when I first click the button an empty NSArray is printed to the output window, but when I press it again it print's "1". It's clear that the program is reaching NSLog before the completion block has time to fully execute. Could someone please help me modify the code so that I have the option to have the NSLog execute after the completion block has finished?

Not sure as to what you are trying to accomplish, but if the goal is to just have NSLog execute after the completion block, you can move the NSLog statement after

[self.users addObject:@"1"];

If you have some code which you want to execute after adding it to the array, you can have

[self methodName]; in the completion block and it will get called there.

Completion block, is the code which is run after execution of the code which you wanted run. The code which you wanted run, will happen asynchronously and on another thread. After that code is run, the completion block code will get executed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM