Cancel All Operations + AFNetworking 3.0
Asked Answered
R

4

7

I have created a class HTTPServiceProvider inherited from AFURLSessionManager. Added below code to get the data.

let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
let manager = AFURLSessionManager(sessionConfiguration: configuration)
let dataTask = manager.dataTaskWithRequest(request) { (response, responseObject, error) in
         //Perform some task
}
dataTask.resume()

I want to add dataTask to operationQueue provided by AFURLSesstionManger and cancel all the operation in some other class (BaseController.swift) before calling the same request again.

Tried this code but not working -

self.operationQueue.addOperationWithBlock{
     //Added above code
}

And inside BaseController.swift file , called -

HTTPServiceProvide.sharedInstance.operationQueue.cancelAllOperations

But its not working :(

Thanks.

React answered 23/8, 2016 at 4:7 Comment(3)
try this in every call before manager.operationQueue.cancelAllOperations()Fortitude
Its not working because without adding any operation to the queue, how you can cancel the operation.React
It may not be possible in this project but it is recommended to use Alamofire instead of AFNetworking when using Swift.Cassareep
I
6

For me best way to cancel all request is:

AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; //manager should be instance which you are using across application
[manager.session invalidateAndCancel];

this approach has one big advantage: all your requests that are executing will call failure block. I mean this one for example:

        [manager GET:url
        parameters:nil
          progress:^(NSProgress * _Nonnull downloadProgress) {
              code
          } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
              code
          } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
              //this section will be executed
          }];

2 things you need to know:

  • it will cancel request performed only with this exact instance of AFHTTPSessionManager you will use for invalidateAndCancel
  • after cancel all requests you have to init new instance of AFHTTPSessionManager - when you tried to make request with old one you will get exception
Ivonne answered 29/12, 2016 at 15:57 Comment(1)
I want to add dataTask inside operationQueue and then cancel all operations inside operationQueueReact
D
3

To cancel all dataTasks with NSURLSession:

manager.session.getTasksWithCompletionHandler { (dataTasks, uploadTasks, downloadTasks) in
    dataTasks.forEach { $0.cancel() }
}
Deuno answered 12/12, 2016 at 11:53 Comment(1)
I want to add dataTask inside operationQueue and then cancel all operations inside operationQueueReact
R
1

I created an array of tasks:

var tasks = [URLSessionDataTask]()

then appended the dataTask which all I want to cancel before making new http request:

tasks.append(dataTask)

And to cancel all the operations, I used:

func cancelPreviousRequest() {
    for task in tasks
       (task as URLSessionTask).cancel()
    }
    tasks.removeAll()
}
React answered 5/1, 2017 at 7:6 Comment(0)
P
1

Maybe use AFURLSessionManager // Invalidates the managed session, optionally canceling pending tasks. - (void)invalidateSessionCancelingTasks:(BOOL)cancelPendingTasks

Isn’t that what you wanted?

Pretzel answered 4/4, 2018 at 9:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.