简体   繁体   English

按需资源 (ODR) 已完成/下载标签的总文件大小

[英]On-demand resources (ODR) get completed/total file size of downloaded tag

I'm downloading zip archives via ODR and want to show a progress and the completed/total file size to the user (like "3/10MB downloaded") Using KVO I managed to observe the fractionCompleted property of the associated Progress object, so I'm able to show a progress bar.我正在通过 ODR 下载 zip 档案,并希望向用户显示进度和已完成/总文件大小(如“3/10MB 下载”) 使用 KVO 我设法观察了关联Progress对象的fractionCompleted属性,所以我'm 能够显示一个进度条。

The code looks like that:代码如下所示:

let request = NSBundleResourceRequest(tags: [tag])

request.progress.observe(\.fractionCompleted, options: [.new, .old]) { [weak self] _, change in
      guard let progress = change.newValue else { return }

      // Use new progress value here
}

Now, to show the completed/total file size in bytes I tried to access completedUnitCount and totalUnitCount , which happens to always have the values 1 (meaning 1 file?).现在,为了以字节为单位显示已完成/总文件大小,我尝试访问completedUnitCounttotalUnitCount ,这恰好始终具有值 1(意味着 1 个文件?)。

How can I obtain the correct values of completedUnitCount and totalUnitCount ?如何获得completedUnitCounttotalUnitCount的正确值?

That was surprise for me as well.这对我来说也是一个惊喜。 We've managed to solve this by using the fractionCompleted property of the Progress value:我们已经通过使用 Progress 值的 fractionCompleted 属性设法解决了这个问题:

progress.fractionCompleted进度.分数完成

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

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