URLSession dataTask 方法返回 0 字节的数据 [英] URLSession dataTask method returns 0 bytes of data

查看:74
本文介绍了URLSession dataTask 方法返回 0 字节的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将 URLSession 的 dataTask 方法与完成处理程序一起使用.响应中的错误是 nil,但是数据对象返回了一些东西,它返回了 0 字节的数据.

I am using URLSession's dataTask method with a completion handler. The error in response is nil, but the data object returns something, it returns 0 bytes of data.

我首先使用 Alamofire 库,我认为它有问题,因为我开始使用较新的版本,所以我声明使用我自己的 Alamofire 实现,这样我就不必重写我已经在我的应用.

I was using Alamofire library firstly, I thought there is something wrong with it because I started using newer version so I stated using my own implementation of Alamofire just so I don't have to rewrite all the calls I already have in my app.

它仍然返回 0 字节的数据.

It still returns 0 bytes of data.

当我通过简单的 URLSession 调用在 Playground 中使用相同的 URL 时,它会工作并返回数据,您知道可能会出现什么问题吗?

When I use the same URL in the Playground with a simple URLSession call, it works and returns the data, do you have any idea what might go wrong?

我对 Alamofire 的实现(在 30 分钟内快速而肮脏):

My implementation of Alamofire (Srsly quick and dirty within 30 minutes):

import Foundation

typealias DataResponse = (data: Data?, response: URLResponse?, error: Error?, request: URLRequest?, result: Result)

public class Alamofire: NSObject {

    enum Method: String {
        case get = "GET"
        case post = "POST"
    }

    fileprivate static let session = URLSession(configuration: URLSessionConfiguration.default)

    public struct request {
        var request: URLRequest? = nil
        var url: URL? = nil
        let method: Alamofire.Method?
        let parameters: Parameters?
        let headers: Headers?

        init(_ request: URLRequest, method: Alamofire.Method? = nil, parameters: Parameters? = nil, headers: Headers? = nil) {
            self.request = request
            self.url = request.url
            self.method = method
            self.parameters = parameters
            self.headers = headers
        }

        init(_ url: URL, method: Alamofire.Method? = nil, parameters: Parameters? = nil, headers: Headers? = nil) {
            self.url = url
            self.request = URLRequest(url: url)
            self.method = method
            self.parameters = parameters
            self.headers = headers
        }
    }
}

typealias Parameters = [String: Any?]
typealias Headers = [String: String]

extension Alamofire.request {

    func responseData(completionHandler: @escaping (_ dataResponse: DataResponse) -> Void) {
        guard let request = request else { return }

        Alamofire.session.dataTask(with: request) { (data, response, error) in
            let result: Result
            if let error = error {
                result = Result.failure(error)
                completionHandler((data, response, error, request, result))
                return
            }

            completionHandler((data, response, error, request, Result.success))
        }.resume()
    }   
}

enum Result {
    case success
    case failure(Error)
}

推荐答案

所以我的问题的解决方案是意识到数据提供者切断了我的数据 :) 也可能发生.这可能不是一个解决方案",而是一个你必须考虑的选项,我会从现在开始:D 谢谢大家

So the resolution to my problem was a realisation that the data provider cut me off of data :) Can happen too. That's probably not a "solution" but an option you have to consider as I will from now on :D Thank you all

这篇关于URLSession dataTask 方法返回 0 字节的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆