Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to cancel network request with afnetworking

Tags:

afnetworking

Is there a way to cancel all network request (the request started by another method) before I do a network request with AFNetworking I tried like below but not work:

- (void)sendRequest:(NSUInteger)page{


NSURL *aUrl = [NSURL URLWithString:@"http://www.abc.com/"];
AFHTTPClient *httpClientToCancel = [[AFHTTPClient alloc] initWithBaseURL:aUrl];
[httpClientToCancel cancelAllHTTPOperationsWithMethod:@"POST" path:@"product/like"];
[httpClientToCancel release];

... start a new request here .....

But not work. I just want to cancel all request (at least the request I wrote above) before I start a new request.

Thank you!

like image 518
Jason Zhao Avatar asked Jun 17 '12 09:06

Jason Zhao


3 Answers

[[httpClient operationQueue] cancelAllOperations];

like image 123
mattt Avatar answered Oct 19 '22 15:10

mattt


Don't Create new AFHTTPClient instance.

try "[self cancelAllHTTPOperationsWithMethod:@"POST" path:@"product/like"];

like image 11
Kaniel Avatar answered Oct 19 '22 14:10

Kaniel


Both the other two answers are right. Don't Create new AFHTTPRequestOperationManager instance

        @interface OperateCustomerView () <WYPopoverControllerDelegate>{

        AFHTTPRequestOperationManager *manager;// = [AFHTTPRequestOperationManager manager];
    }
- (void)viewDidLoad {
    [super viewDidLoad];
    manager = [AFHTTPRequestOperationManager manager];
like image 1
Gank Avatar answered Oct 19 '22 16:10

Gank