Alamofire在分段上传中不会产生网络丢失/超时错误

时间:2017-06-29 02:29:09

标签: ios swift alamofire

    let configuration = URLSessionConfiguration.default
    configuration.timeoutIntervalForRequest = 15.0
    configuration.timeoutIntervalForResource = 15.0
    self.alamofireManager = Alamofire.SessionManager(configuration: configuration)


self.alamofireManager.upload(multipartFormData: { (multipartFormData) in
        multipartFormData.append(UIImageJPEGRepresentation(newImage, 1)!, withName: "file", fileName: "image.jpeg", mimeType: "image/jpeg")
        if let parameters = params{
            for (key, value) in parameters {
                multipartFormData.append(value.data(using: String.Encoding.utf8.rawValue)!, withName: key)
            }
        }
    }, to:apiURL, method: .post, headers: authHeader)
    { (result) in
        switch result {
        case .success(let upload, _, _):
            upload.uploadProgress(closure: { (progress) in
                DDLogInfo("Image Upload Progress : \(progress)")
            })
            upload.responseJSON { response in
                //print response.result
            }
            break
        case .failure(let error):
            if error._code == NSURLErrorTimedOut || error._code == NSURLErrorNetworkConnectionLost {
                print("Time Out/Connection Lost Error")
            }
            DDLogInfo("Error :\(error.localizedDescription)")
            break
        }
    }

这里我使用的是默认会话行为,但仍然没有因超时或网络连接丢失而出现任何错误。

我在启动请求后断开了互联网连接,但没有错误。

1 个答案:

答案 0 :(得分:1)

就像我在GitHub上回复一样,这是由于你对尾随闭包中 <li class="list"> <div class="dropdown" data-load="true"> <span class="dropdown-toggle"></span> <ul class="dropdown-menu" role="menu"> <li> <a class="list-name" href="www.test.com" target='_blank' data-toggle="tooltip" data-placement="top" data-original-title="">some value</a> </li> </ul> </div> </li> 的混淆。这是多部分编码的结果,而不是网络请求。要获取网络请求状态,您需要在result上使用responseJSON进行检查。