Alamofire暂停请求不要在队列中连接其他请求

时间:2016-08-08 15:00:39

标签: ios alamofire

我正在使用Alamofire库制作下载管理器。

  • 我正在配置Alamofire Manager HTTPMaximumConnectionsPerHost = 4

  • 我开始下载5个项目,然后4个项目开始下载和 第五项正在排队等候。

  • 当我暂停第4项中的一项时,我期待第5项 开始下载。这没有发生,这是我的问题

这是我的代码:

func startDownload(thisItem url: String, folderName: String) {

let startDownloadRequest = AlamofireManager!.download(.GET, url,
    destination: { temporaryURL, response in
        let fileManager = NSFileManager.defaultManager()
        let directoryURL = fileManager.URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0]
        self.createFolderWithName(folderName)
        let pathComponent = "\(folderName)/\(response.suggestedFilename!)"            
        return directoryURL.URLByAppendingPathComponent(pathComponent)
})
    .progress { bytesRead, totalBytesRead, totalBytesExpectedToRead in
        dispatch_async(dispatch_get_main_queue())
        {
            let progress = String(Float(totalBytesRead) / Float(totalBytesExpectedToRead))
        }
    }
    .response { _, _, _, error in
        if let error = error {
            print("Failed with error: \(error)")
        } else {
            print("Downloaded file successfully")
            }
        }
}

这是暂停请求的方法

    func pauseDownload(thisItem item: String) {
    var request : Request
    for req in DownloadCenter.defaultCenter.currentDownloadsList {
        if req.request?.URLString == item {
            request = req
            request.suspend()
            break
        }
    }
}

这是我的情况: 开始下载5个项目,4个正在下载,第5个项目正在等待.. enter image description here

暂停后...... enter image description here

0 个答案:

没有答案