[英]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.