Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alamofire 4 FAILURE: Error Domain=NSURLErrorDomain Code=-999 “cancelled” #2178

Closed
Lves opened this issue Jun 26, 2017 · 1 comment
Closed
Assignees
Labels

Comments

@Lves
Copy link

Lves commented Jun 26, 2017

When update to Alamofire4 and Swift3 ,request sometimes be cancled.

open override func requestJSON() -> SignalProducer<NetApiProtocol, NetError> {
        NetApiData.addApi(self)
        return SignalProducer { [unowned self] sink,disposable in
            let urlRequest = NetApiData.getURLRequest(self)
            let request = Alamofire.request(urlRequest)
            self.request = request
            self.indicator?.bindTask(request.task!)
            let timeBegin = Date()
            request.responseJSON { [weak self] response in
                guard let wself = self else { return }
                NetApiData.removeApi(wself)
                DDLogWarn("请求耗时: \(NSDate().timeIntervalSince(timeBegin).secondsToHHmmssString())")
                let transferedResponse = wself.transferResponseJSON(response.toNetApiResponse())
                switch transferedResponse.result {
                case .success:
                    DDLogInfo("请求成功: \(wself.url)")
                    if let value = transferedResponse.result.value {
                        DDLogVerbose("JSON: \(value)")
                        wself.response = response.toNetApiResponse()
                        wself.fillJSON(value)
                        sink.send(value: wself)
                        sink.sendCompleted()
                        return
                    }
                case .failure(let error):
                    if let statusCode = StatusCode(rawValue:error.code) {
                        switch(statusCode) {
                        case .canceled:
                            DDLogWarn("请求取消: \(wself.url)")
                            sink.sendInterrupted()
                            return
                        default:
                            break
                        }
                    }
                    DDLogError("请求失败: \(wself.url)")
                    DDLogError("\(error)")
                    let err = error is NetError ? error as! NetError : NetError(error: error)
                    err.response = transferedResponse.response
                    sink.send(error: err)
                }
            }
            disposable.add { [weak self] in
                guard let wself = self else { return }
                NetApiData.removeApi(wself)
            }
        }
    }

when i delete this line self.indicator?.bindTask(request.task!) ,it will be ok.
bindTask coder is:

open func bindTask(_ task: URLSessionTask, view: UIView?, text: String?){
        task.indicatorView = view
        task.indicatorText = text ?? ""
        let notificationCenter = NotificationCenter.default
        notificationCenter.removeObserver(self, name: Notification.Name.Task.DidResume, object: nil)
        notificationCenter.removeObserver(self, name: Notification.Name.Task.DidSuspend, object: nil)
        notificationCenter.removeObserver(self, name: Notification.Name.Task.DidCancel, object: nil)
        notificationCenter.removeObserver(self, name: Notification.Name.Task.DidComplete, object: nil)
        if task.state == .running {
            notificationCenter.addObserver(self, selector: #selector(TaskIndicator.task_resume), name: Notification.Name.Task.DidResume, object: nil)
            notificationCenter.addObserver(self, selector: #selector(TaskIndicator.task_suspend), name: Notification.Name.Task.DidSuspend, object: nil)
            notificationCenter.addObserver(self, selector: #selector(TaskIndicator.task_cancel), name: Notification.Name.Task.DidCancel, object: nil)
            notificationCenter.addObserver(self, selector: #selector(TaskIndicator.task_end), name: Notification.Name.Task.DidComplete, object: nil)
            var notify = Notification(name: Notification.Name(rawValue: ""), object: nil)
            notify.userInfo = [Notification.Key.Task: task]
            self.task_resume(notify: notify)
        } else {
            var notify = Notification(name: Notification.Name(rawValue: ""), object: nil)
            notify.userInfo = [Notification.Key.Task: task]
            self.task_end(notify: notify)
        }
    }

I call request in viewWillAppear,if I did not delete self.indicator?.bindTask(request.task!) , the first is ok , second is bad.I want the reason?

Async.main(after: 0.5) { [weak self] in self?.loadData() }
2.
loadData()

@cnoon
Copy link
Member

cnoon commented Jun 26, 2017

Hi @Lves,

I'd suggest you open a question on Stack Overflow and tag alamofire. We use our GitHub project for bug reports and feature requests.

Best of luck! 🍻


From our Contribution Guidelines

Asking Questions

We don't use GitHub as a support forum. For any usage questions that are not specific to the project itself, please ask on Stack Overflow instead. By doing so, you'll be more likely to quickly solve your problem, and you'll allow anyone else with the same question to find the answer. This also allows maintainers to focus on improving the project for others.

@cnoon cnoon closed this as completed Jun 26, 2017
@cnoon cnoon self-assigned this Jun 26, 2017
@cnoon cnoon added the support label Jun 26, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants