2013-11-22 3 views
36

네트워킹 기능을 AFNetworking에서 AFNetworking v2으로 변경하고 AFHttpClient 대신 AFHTTPRequestOperationManager을 사용하여 iOS6을 지원합니다.AFNetworking 2 : AFHTTPRequestOperationManager 요청을 취소하는 방법은 무엇입니까?

내 문제는 AFHttpClient에가있는 동안 기능이 AFHTTPRequestOperationManager에서 그러한 명백한 방법이 없다는

- (void)cancelAllHTTPOperationsWithMethod:(NSString *)method path:(NSString *)path; 

방법을 사용하여 보류중인 요청을 취소 할 수 있다는 것입니다. 내가 요청 코드를 만들 때 지금 AFHTTPRequestOperationManager를 서브 클래스와 바르

AFHTTPRequestOperation *_currentRequest; 

를 선언한다까지했던 어떤

- (void)GetSomething:(NSInteger)ID success:(void (^)(MyResponse *))success failure:(void (^)(NSError *))failure 
{ 
    _currentRequest = [self GET:@"api/something" parameters:@{@"ID": [NSNumber numberWithInteger:ID]} success:^(AFHTTPRequestOperation *operation, id responseObject) { 
     MyResponse *response = [MyResponse responseFromDictionary:responseObject]; 
     success(response); 
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
     failure(error); 

    }]; 
} 

같은 내가이야

- (void)cancelCurrentRequest; 

모든 방법은

입니다.
- (void)cancelCurrentRequest 
{ 
    if(_currentRequest) { 
     [_currentRequest cancel]; _currentRequest = nil; 
    } 
} 

6,지금, 나는 이것이 좋은 방법이라고 생각하지 않으며 메서드를 호출 할 때 나는이 올바르게 수행하기에 대한 몇 가지 조언이 필요한 이유입니다 (NSURLErrorDomain error -999.)를 얻을.

미리 감사드립니다. 당신이 요청을 보낼 때 때문에

답변

79
[manager.operationQueue cancelAllOperations]; 
+3

들으. 특정 GET/POST 요청을 취소하려면 어떻게해야합니까? – ozzotto

+3

cancelAllHTTPOperationsWithMethod : path 구현을 살펴보면 어떻게 수행하는지 알 수 있습니다. operationQueue의 모든 작업을 반복하면 메서드와 경로와 일치하는 항목을 찾아 취소 할 수 있습니다. 그들이 v2에서 제거한 이유는 특정 작업을 취소 할 필요가 없다고 생각하기 때문입니다. –

+0

그게 내가 끝내 준거야. 내 AFHTTPRequestOperationManager 하위 클래스의 cancelAllHTTPOperationsWithMethod : path 구현입니다. 너 친구 야. – ozzotto

2

당신은 단순히 요청을해야 할 때 cancel을 수행 한 후 어딘가에 저장 또는 정적하고, AFHTTPRequestOperationManager를 서브 클래 싱하기 위해

- (AFHTTPRequestOperation *)GET:(NSString *)URLString 
        parameters:(id)parameters 
         success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success 
         failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure 

에서 AFHTTPRequestOperation 반환하지 않습니다 취소 된.

예 : 답변

- (void)sendRequestToDoSomething 
{ 
    static AFHTTPRequestOperation *operation; 
    if(operation) //cancel operation if it is running 
     [operation cancel]; 
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; 
    //configure manager here.... 

operation = [manager GET:urlString parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) { 
    //do something here with the response 
    operation = nil; 
} failure:^(AFHTTPRequestOperation *op, NSError *error) { 
{ 
    //handle error 
    operation = nil; 
} 
관련 문제