整个会话下载进度Alamofire

时间:2015-12-14 09:36:08

标签: ios swift nsurlsession alamofire

Alamofire被用于在一次请求下载多个较大的文件。 我可以使用

单独查看每个文件的进度
Alamofire.request(.GET, imageURL).progress

但我想立刻跟踪所有公开会议的进度,不知道该怎么做。 (假设我有15个文件同时下载) 我阅读了很多教程来解决单个文件的进度,但整个会话没有。

是否有可能通过Alamofire跟踪进展情况,如果是,如何进行?

1 个答案:

答案 0 :(得分:4)

在iOS9中,您可以创建自己的NSProgress对象,并观察,例如fractionCompleted。然后你可以addChild

private var observerContext = 0

class ViewController: UIViewController {

    private var progress: NSProgress!

    override func viewDidLoad() {
        super.viewDidLoad()

        progress = NSProgress()
        progress.addObserver(self, forKeyPath: "fractionCompleted", options: .New, context: &observerContext)

        downloadFiles()
    }

    deinit {
        progress?.removeObserver(self, forKeyPath: "fractionCompleted")
    }

    private func downloadFiles() {
        let filenames = ["as17-134-20380.jpg", "as17-140-21497.jpg", "as17-148-22727.jpg"]

        let baseURL = NSURL(string: "http://example.com/path")!

        progress.totalUnitCount = Int64(filenames.count)
        progress.completedUnitCount = 0

        for filename in filenames {
            let url = baseURL.URLByAppendingPathComponent(filename)
            let childProgress = Alamofire.request(.GET, url.absoluteString)
                .response() { request, response, data, error in
                    // process response
                }
                .progress
            progress.addChild(childProgress, withPendingUnitCount: 1)
        }
    }

    override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
        if context == &observerContext {
            if keyPath == "fractionCompleted" {
                let percent = change![NSKeyValueChangeNewKey] as! Double
                print("\(percent)")
            }
        } else {
            super.observeValueForKeyPath(keyPath, ofObject: object, change: change, context: context)
        }
    }

}

如果您还需要iOS 7/8支持,可以拨打becomeCurrentWithPendingUnitCountresignCurrent

for filename in filenames {
    let url = baseURL.URLByAppendingPathComponent(filename)
    progress.becomeCurrentWithPendingUnitCount(1)
    Alamofire.request(.GET, url.absoluteString)
        .response() { request, response, data, error in
            // process response
    }
    progress.resignCurrent()
}

如果您使用的是AFNetworking,则它是相同的过程(即上述相同的viewDidLoadobserveValueForKeyPathdeinit方法),而不是检索Alamofire progress属性,您改为使用AFHTTPSessionManager方法downloadProgressForTask来获取与NSProgress相关联的NSURLSessionTask。例如:

private func getFiles() {
    let filenames = ["as17-134-20380.jpg", "as17-140-21497.jpg", "as17-148-22727.jpg"]

    let manager = AFHTTPSessionManager()
    manager.responseSerializer = AFHTTPResponseSerializer()

    let baseURL = NSURL(string: "http://example.com/path")!

    progress.totalUnitCount = Int64(filenames.count)
    progress.completedUnitCount = 0

    for filename in filenames {
        let url = baseURL.URLByAppendingPathComponent(filename)
        let task = manager.GET(url.absoluteString, parameters: nil, progress: nil, success: { task, responseObject in
            // do something with responseObject
            print(url.lastPathComponent! + " succeeded")
        }, failure: { task, error in
            // do something with error
            print(error)
        })

        if let downloadTask = task, let childProgress = manager.downloadProgressForTask(downloadTask) {
            progress.addChild(childProgress, withPendingUnitCount: 1)
        }
    }
}

或者,如果使用下载任务:

private func downloadFiles() {
    let filenames = ["as17-134-20380.jpg", "as17-140-21497.jpg", "as17-148-22727.jpg"]

    let manager = AFHTTPSessionManager()

    let baseURL = NSURL(string: "http://example.com/path")!

    progress.totalUnitCount = Int64(filenames.count)
    progress.completedUnitCount = 0

    let documents = try! NSFileManager.defaultManager().URLForDirectory(.DocumentDirectory, inDomain: .UserDomainMask, appropriateForURL: nil, create: false)

    for filename in filenames {
        let url = baseURL.URLByAppendingPathComponent(filename)
        let task = manager.downloadTaskWithRequest(NSURLRequest(URL: url), progress: nil, destination: { (temporaryURL, response) -> NSURL in
            return documents.URLByAppendingPathComponent(url.lastPathComponent!)
        }, completionHandler: { response, url, error in
            guard error == nil else {
                print(error)
                return
            }

            if let name = url?.lastPathComponent {
                print("\(name) succeeded")
            }
        })

        if let childProgress = manager.downloadProgressForTask(task) {
            progress.addChild(childProgress, withPendingUnitCount: 1)
        }

        task.resume()
    }
}