Frage

I want to stop/cancel the operation in case of running the request again. Method cancelAllHTTPOperationsWithMethod is working ok, but i have a problem when AFNetworking has already fetched the results and my successBlock is being fired - I want to stop it in the nearest future. But the problem is that operation.isCancelled is not cancelled.

The question is do i have to perform my 'very long successBlock' in NSOperation and cancel them too or is there any easier and faster method?

Code:

[[AFHTTPClient sharedInstance] cancelAllHTTPOperationsWithMethod:@"GET" path:@"path"];
[[AFHTTPClient sharedInstance] getPath:@"path" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void) {
        for (longLoop) {
            // do something long 
            if (self.isCancelled) return; // this won't fire no matter how often i run it
        }
    });

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    // do something to fail
}];
War es hilfreich?

Lösung

I've ended with doing NSOperation inside. Something like:

[[AFHTTPClient sharedInstance] cancelAllHTTPOperationsWithMethod:@"GET" path:@"path"];
[operationQueue cancelAllOperations];

[[AFHTTPClient sharedInstance] getPath:@"path" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
    // add new operation
    MyOperation *successOperation = [[MyOperation alloc] init];
    [successOperation setResponseObject:responseObject];
    [operationQueue addOperation:successOperation];

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    // call delegate
    [self didFailFetchDataWithError:error];
}];
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top