简体   繁体   English

URLSession委托方法不起作用

[英]URLSession delegate methods don't works

I'm going to read file from API, but its size equal 1.6mb and it takes so much time. 我将从API读取文件,但文件大小为1.6mb,需要花费大量时间。 I wish to read it by parts, and when i founds data which i needs, i'm going to stop recieve data. 我希望按部就班地阅读它,当我找到需要的数据时,我将停止接收数据。 I trying to use some delegate methods, but they don't works. 我尝试使用一些委托方法,但它们不起作用。 I don't understand what goes wrong? 我不明白怎么了?
I have next code: 我有下一个代码:

class ViewController: UIViewController, URLSessionTaskDelegate, URLSessionDelegate, URLSessionDataDelegate {

    var httpString = "hided"

    override func viewDidLoad() {
        super.viewDidLoad()
        getLogBinData()
    }

    func getLogBinData() {
        let session = URLSession(configuration: .default, delegate: self, delegateQueue: OperationQueue.main)
        if let url = URL(string: httpString + "log.bin") {
            var request = URLRequest(url: url)
            request.httpMethod = "GET"
            let task = session.dataTask(with: request)
            task.resume()
        }
    }

    func urlSession(_ session: URLSession, task: URLSessionTask, didSendBodyData bytesSent: Int64, totalBytesSent: Int64, totalBytesExpectedToSend: Int64) {
        print()
    }

    func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data) {
        if dataTask.countOfBytesReceived >= 500 {
            print(dataTask.countOfBytesReceived)
        }
    }

    func urlSession(_ session: URLSession,
                    dataTask: URLSessionDataTask,
                    didReceive response: URLResponse,
                    completionHandler: @escaping (URLSession.ResponseDisposition) -> Void) {
        if dataTask.countOfBytesReceived >= 500 {
            print(dataTask.countOfBytesReceived)
        }
    }


}

according the comment, i edited code and it's worked. 根据评论,我编辑了代码,它就起作用了。

class ViewController: UIViewController, URLSessionTaskDelegate, URLSessionDelegate, URLSessionDataDelegate {

    var httpString = "hided"

    override func viewDidLoad() {
        super.viewDidLoad()
        getLogBinData()
    }

    func getLogBinData() {
        let session = URLSession(configuration: .default, delegate: self, delegateQueue: OperationQueue.main)
        if let url = URL(string: httpString + "log.bin") {
            var request = URLRequest(url: url)
            request.httpMethod = "GET"
            let task = session.dataTask(with: request)
            task.resume()
        }
    }

    func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data) {
        if dataTask.countOfBytesReceived >= 500 {
            print(dataTask.countOfBytesReceived)
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 URLSession委托未调用的成功方法,但没有错误 - URLSession delegate success methods not called, but no error 自定义注释的属性在委托方法中不可见 - Properties of custom Annotation don't visible in delegate methods 要以编程方式滚动UIScrollView,但不只在委托方法中处理此操作? - To scroll UIScrollView programmatically but don't handle only this action in delegate methods? 为什么 UICollectionView 的委托方法不起作用? - Why does the UICollectionView's delegate methods don't work? 为什么不调用表视图委托方法? - Why the table view delegate methods don't get called? tabBarController将委托方法发送到不响应它们的选项卡 - tabBarController sending delegate methods to tabs that don't respond to them XCTest CLLocationManager的委托方法不会被调用 - XCTest CLLocationManager's delegate methods don't get called UIScrollViewDelegate 中的委托方法不响应表视图滚动事件 - Delegate methods in UIScrollViewDelegate don't respond to table view scrolling event Swift:UIViewController 实例未从 URLSession 委托方法接收更新 - Swift: UIViewController instance not receiving updates from URLSession Delegate methods URLsession委托函数出错 - Error with URLsession delegate function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM