繁体   English   中英

NSProgress显示完成时无法读取文件?

[英]Can't read file when NSProgress shows complete?

我使用[manager downloadTaskWithRequest:request progress:&progress destination:^NSURL *(NSURL *targetPath, NSURLResponse *response){}]进行下载。

我使用KVO读取了当progress.fractionCompleted == 1时下载的文件。 而且我发现在读取文件时我得到的结果是

因此,我想知道何时可以读取文件。 完成进度意味着完全在磁盘上写入文件或仅接收所有数据

编辑

- (void)URLSession:(__unused NSURLSession *)session
      downloadTask:(__unused NSURLSessionDownloadTask *)downloadTask
      didWriteData:(__unused int64_t)bytesWritten
 totalBytesWritten:(int64_t)totalBytesWritten
totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite
{
    self.progress.totalUnitCount = totalBytesExpectedToWrite;
    self.progress.completedUnitCount = totalBytesWritten;
}

那么,应该先写入文件然后设置进度吗? 但是为什么我得到零?

如果要向用户显示某种进度,可以跟踪委托方法中的进度。 但这不是您知道何时获取文件的方式。

知道何时可以获取文件的方法是何时触发委托方法URLSession:downloadTask:didFinishDownloadingToURL: 您必须使用该委托方法来获取文件。

而且您必须立即执行 您不能在两者之间使用KVO,因为在从代理方法触发到通过KVO通知您的那一刻之间,文件可能会被破坏。 当我们URLSession:downloadTask:didFinishDownloadingToURL: 返回时,它被销毁。 这就是为什么您必须该方法内部而不是其他地方使用它。

暂无
暂无

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

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