2014-09-10 1 views
1

mac 응용 프로그램에서 AFNetworking (2.4.1)을 사용하고 있습니다. 모든 다른 작업 (AFHTTPRequestOperation) 완료 후 내 자신의 블록 작업을 추가하고 싶습니다. completionOperation과 다른 것들 사이에 의존성을 추가하려고 시도했지만, completionOperation 블록은 다른 것들이 성공 또는 실패로 완료되기 전에 여전히 실행됩니다.AFHTTPRequestOperation을 사용할 때 작업 간의 종속성 만들기

기본 사항을 보여주는 코드의 축소 버전이 아래에 나와 있습니다. 누구든지이 작품을 만드는 방법을 제안 할 수 있습니까? 감사.

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]; 
+0

들여 쓰기를 수정하십시오. –

+0

더 이상 유지 관리되지 않는 AFHTTPRequest를 사용하지 않기를 바랍니다. – SeanChense

+0

안녕하세요 @SeanChense. 내가 사용하고있는 특정 클래스 또는 프레임 워크를 의미합니까? 여기서 사용하는 클래스는 문서의 AFNetworking 배치 예제에서 사용되는 클래스입니다. batchOfRequestOperations를 사용하지 않는다는 점을 제외하고는 제공된 예제와 매우 흡사합니다. 조작간에 내 자신의 종속성을 설정하려고했기 때문입니다. – Chris

답변

1
- (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]; 

} 
+0

_how_이 질문에 대한 답변을 설명해 주시겠습니까? – Ben

+0

예 확실! 그래서 여기에 내 예제에서는 두 블록으로 작업하고 있었는데 첫 번째 작업은 두 번째 작업이 끝나고 DEPENDECY와 함께 완료되었습니다. "[operation addDependency : operation2] [operationQueue addOperations : @ [operation, operation2] waitUntilFinished : YES] ; " 내가 너를 도울 수 있기를 바란다. –