iOS Alamofire stop all requests

前端 未结 8 1588
刺人心
刺人心 2020-12-05 07:45

Is there any way I can for example say:

Alamofire.Manager.cancelAllRequests() or Alamofire.Manager.sharedInstance.cancelAllRequests()?

8条回答
  •  眼角桃花
    2020-12-05 08:11

    cnoon's one-liner solution is great but it invalidates the NSURLSession and you need to create a new one.

    Another solution would be this (iOS 7+):

    session.getTasks { dataTasks, uploadTasks, downloadTasks in
        dataTasks.forEach { $0.cancel() }
        uploadTasks.forEach { $0.cancel() }
        downloadTasks.forEach { $0.cancel() }
    }
    

    Or if you target iOS 9+ only:

    session.getAllTasks { tasks in
        tasks.forEach { $0.cancel() }
    }
    

提交回复
热议问题