简体   繁体   English

Alamofire履历未按预期工作

[英]Alamofire resume not working as expected

I´m trying to download a file, and I want to detect when it loses connectivity using NetworkReachabilityManager like so: 我正在尝试下载文件,我想使用NetworkReachabilityManager来检测何时失去连接:

self.reachabilityManager?.listener = { status in
        print("Status: \(status)")
        if status == .NotReachable {
            self.download?.suspend()
        }
        else if status == .Reachable(.EthernetOrWiFi) {
            self.download?.resume()
    }
    self.reachabilityManager?.startListening()

Initially I start the download like this: 最初,我像这样开始下载:

self.download = self.manager.download(.GET, url) { (temporaryURL, response) -> NSURL in
        return NSURL(string: fullFilename)!
        }
        .progress { (bytesRead, totalBytesRead, totalBytesExpectedToRead) in
            self.setProgress(totalBytesRead, totalBytesExpectedToRead: totalBytesExpectedToRead)
        }
        .response { (request, response, data, error) in
            self.handleDownloadResponse(fullFilename, response: response, data: data, error: error)
    }

When I switch the network off, I would think that self.download?.suspend() would suspend the download request, and afterwards self.download?.resume() would restart the download, but the progress handler is never called, and after a while the response handler fires up with a timeout error 当我关闭网络时,我认为self.download?.suspend()将暂停下载请求,然后self.download?.resume()将重新开始下载,但是从不调用进度处理程序,之后一会儿,响应处理程序因超时错误而启动

Should I be doing this differently? 我应该这样做吗? How can I achieve this behaviour (resume the download when network is restored)? 如何实现此行为(在恢复网络后恢复下载)?

You need to make quite a few changes to this logic for things to work as intended. 您需要对此逻辑进行很多更改,以使事情按预期工作。 Here are a set of steps to get you where you want. 这是一组步骤,可帮助您到达所需的位置。

  1. When you lose connectivity with the request in-flight, your response closure will be called and the error will indicate a connectivity issue. 当您在进行中与请求的连接断开时,将调用您的response关闭,并且该error将指示连接问题。
  2. In the response closure, if the data parameter is not nil , it is the resumeData of the download request and can be used to resume the download from where it left off. response结束中,如果data参数不是nil ,则它是下载请求的resumeData ,可用于从中断处恢复下载。
  3. If the data parameter is nil , then you'll need to restart the download since it can't be resumed. 如果data参数为nil ,那么您将需要重新开始下载,因为无法恢复下载。
  4. In the connectivity closure, you only need to check the isReachable or isReachableOnEthernetOrWiFi parameters. 在连接性关闭中,您仅需要检查isReachableisReachableOnEthernetOrWiFi参数。
  5. Inside the reachability check, you need to make a new download request with the resumeData if you have it, and a completely new request if you don't. 在可达性检查中,如果需要,则需要使用resumeData发出一个新的下载请求,如果没有,则需要一个全新的请求。

You cannot run the same download request again once the response closure has been called. 一旦调用了response关闭,就无法再次运行相同的下载请求。

I haven't tried to use suspend and resume on download requests in the way you are. 我没有尝试以您的方式对下载请求使用suspendresume The docs do seem to imply that it's possible to use suspend and resume to do what you're doing, but the common practice is to cancel the request, then create a new one with the resume data. 该文档似乎暗示着可以使用suspendresume来做您正在做的事情,但是通常的做法是取消请求,然后使用恢复数据创建一个新请求。 You'd also have to do it before the request would fail which I'm assuming is not possible. 在请求失败之前,您还必须这样做,我认为这是不可能的。

I would guess that your requests have already failed by the time the reachability closure is called. 我猜想在调用可达性关闭时,您的请求已经失败了。

Hopefully that helps get you on the right track. 希望这有助于您走上正确的道路。 For more information on any of those steps, I'd refer to the URL Loading System documentation along with the Alamofire README . 有关任何这些步骤的更多信息,请参考URL加载系统文档以及Alamofire README

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM