Alamofire: [Result]: FAILURE: Error Domain=NSURLErrorDomain Code=-999 “cancelled”

懵懂的女人 提交于 2019-12-05 02:19:32

To retain SessionManager instance you need to capture it in closure passed to responseJSON:

sessionManager.request("https://sub.url.com/path", headers: headers).responseJSON { response in
    let _ = sessionManager // retain
    // ...
}

Otherwise sessionManager is deallocated shortly it goes out of scope and any executing requests are cancelled.

Please add this statement to the end of responseJson block:

manager.session.invalidateAndCancel()

It happens if the object of the manager is not retained till execution of the block completes, so this would ensure its retention.

Cheers!

john7ric

Please check in sessiondidReceiveChallenge: delegate implementation of NSURLSession. Chances are NSURLSessionAuthChallengeCancelAuthenticationChallenge is getting executed somewhere.

self.getSessionManager().request(urlstring, method: methods, parameters: parameters, encoding: JSONEncoding.prettyPrinted, headers: Header).responseJSON(queue: nil, options: JSONSerialization.ReadingOptions.allowFragments) { (responseObject) in ... .... }.session.finishTasksAndInvalidate()

Just put the method of invalidate after task finish, means session.finishTasksAndInvalidate()

You need to properly manage the lifetime of your SessionManager instance. Most commonly this is done by making a singleton instance. Otherwise, when your manager goes out of scope and is deinited, all outstanding requests will be cancelled.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!