How can I cancel all afhttpsessionmanagers requests in the background?

I had an application in which I call synchronized requests using session managers, in

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

      

I am using global

@property(strong,nonatomic)AFHTTPSessionManager *manager;

      

and highlighting in viewdidload like

manager = [[AFHTTPSessionManager alloc] init];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    manager.responseSerializer.acceptableContentTypes =  [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html", nil];
    AFJSONResponseSerializer *responseSerializer = [AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingAllowFragments];

    [manager setResponseSerializer:responseSerializer];

      

and the group of requests is executed in a loop. there is an option for me to cancel all requests for me where i do

for (manager in self.arrayOfTasks) {

        [manager invalidateSessionCancelingTasks:YES];
    }




    manager=nil;

      

but the problem is that after canceling also these requests are repeated over and over again. This is not cancelling.can anyone help me find where i am going wrong?

+3


source to share


1 answer


You can try this below code to cancel all started tasks for AFHTTPSession Manager:

 for (NSURLSessionTask *task in manager.tasks)
    {
        [task cancel];
    }

      

Also for individual loading and task loading:

Loading a task:



 for (NSURLSessionTask *task in manager.downloadTasks)
        {
            [task cancel];
        }

      

Canceling a download cancellation:

 for (NSURLSessionTask *task in manager.uploadTasks)
        {
            [task cancel];
        }

      

+1


source







All Articles