Chris
Chris

Reputation: 11

Creating dependencies between operations when using AFHTTPRequestOperation

I'm working with AFNetworking (2.4.1) in a mac application. I'm hoping to add my own block operation that is for after completion of all of the other operations (which are AFHTTPRequestOperation). I have tried adding dependencies between the completionOperation and the others, but the completionOperation block still executes before the others have completed with success or failure.

A cut down version of the code that illustrates the basics is below. Is anyone able to suggest how to make this work? Thanks.

AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] init];

    NSBlockOperation *completionOperation = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"All operations complete");
        }];


    NSMutableURLRequest *request1 = [[AFHTTPRequestSerializer serializer] requestWithMethod:@"GET" URLString:[[NSURL URLWithString:@"https://api.parse.com/1/classes/SomeClass"] absoluteString] parameters:nil error:nil];
    AFHTTPRequestOperation *operation1 = [manager HTTPRequestOperationWithRequest:request1 success:
    ^(AFHTTPRequestOperation *operation, id responseObject)
    {
            NSLog(@"operation 1 success");
    }
    failure:^(AFHTTPRequestOperation *operation, NSError *error)
    {
            NSLog(@"operation 1 failure");
    }];

    NSMutableURLRequest *request2 = [[AFHTTPRequestSerializer serializer] requestWithMethod:@"GET" URLString:[[NSURL URLWithString:@"https://api.parse.com/1/classes/OtherClass"] absoluteString] parameters:nil error:nil];
    AFHTTPRequestOperation *operation2 = [manager HTTPRequestOperationWithRequest:request2 success:
    ^(AFHTTPRequestOperation *operation, id responseObject)
    {
            NSLog(@"operation 2 success");
    }
    failure:^(AFHTTPRequestOperation *operation, NSError *error)
    {
            NSLog(@"operation 2 failure");
    }];

    [completionOperation addDependency:operation1];
    [completionOperation addDependency:operation2];

    [manager.operationQueue addOperation:operation1];
    [manager.operationQueue addOperation:operation2];
    [manager.operationQueue addOperation:completionOperation];

Upvotes: 1

Views: 217

Answers (1)

sarra.srairi
sarra.srairi

Reputation: 150

    - (void) test3:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
                     failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure

{

  // block 1
    NSString *string = [NSString stringWithFormat:@"%@weather.php?format=json", BaseURLString];
    NSURL *urll = [NSURL URLWithString:string];
    NSURLRequest *request = [NSURLRequest requestWithURL:urll];
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    operation.responseSerializer = [AFJSONResponseSerializer serializer];

    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"OPERATION 1  %@",responseObject );
        test_Sync = @"done";
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        test_Sync = @"faile"; }];

    //block 2
    NSString *string2 = [NSString stringWithFormat:@"%@weather.php?format=json", BaseURLString];
    NSURL *urll2 = [NSURL URLWithString:string2];
    NSURLRequest *request2 = [NSURLRequest requestWithURL:urll2];

    AFHTTPRequestOperation *operation2 = [[AFHTTPRequestOperation alloc] initWithRequest:request2];

    [operation2 setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation2, id responseObject) {
        // Print the response body in text
        NSLog(@"Response: OPERATION 2 %@", [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]);
    } failure:^(AFHTTPRequestOperation *operation2, NSError *error) {
        NSLog(@"Error: %@", error);
    }];

    // Add the operation to a queue
    // It will start once added
    NSOperationQueue *operationQueue = [[NSOperationQueue alloc] init];
    // Make operation2 depend on operation1
    [operation addDependency:operation2];
    [operationQueue addOperations:@[operation, operation2] waitUntilFinished:YES];

}

Upvotes: 1

Related Questions