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

标签 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
        }
    }

这里我使用了默认的 session 行为,但仍然没有收到任何超时或网络连接丢失的错误。

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

最佳答案

就像我在 GitHub 上的回复一样,这是因为您对尾随闭包中的 result 感到困惑。这是多部分编码的结果,而不是网络请求。要获取网络请求状态,您需要检查您在 upload 上使用的 responseJSON

关于ios - Alamofire 不会在分段上传中生成网络丢失/超时错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44815413/

相关文章:

ios - 尝试在 alamofire 中发送 json 对象,但它位于 json 数组中

ios - NSURLCache Alamofire 删除缓存响应

ios - swift 将文件类型与 iOS 应用程序相关联

iOS 应用程序 - 更改版本号方案

ios - 在 Swift 中,如何将 "°"转换为 "°"(即度数符号)?使用内置的 Cocoa 实用程序(使用 NSAttributedString)

objective-c - 禁用 NSDocument 的 "Revert To"和 "Duplicate"菜单项

ios - 使用 SwiftGen 在运行时更改语言

swift - 在键入时关闭 Xcode 的未使用变量警告

ios - Xcode 10.1 : Provisioning profile signing certificate error

ios - 有没有办法在 Alamofire api 请求的响应等待期间显示事件指示器或进度条?使用 swift