ios - 为多个文件下载添加子进度
<p><p>我正在我的项目中使用 AFNetworking 3.0 下载多个文件。我想显示所有文件的单个下载进度。我将每个文件下载的每个子进度添加到父进度中。但它不起作用,应用程序崩溃了。我收到了错误 -</p>
<pre><code>Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '<DownloadManager: 0x7f92e2f6e130>: An -observeValueForKeyPath:ofObject:change:context: message was received but not handled.
Key path: fractionCompleted
</code></pre>
<p>这是我的下载类-</p>
<pre><code>@interface DownloadManager ()
@property (nonatomic, strong) NSProgress *progress;
@property (nonatomic, strong) Settings *settings;
@property (nonatomic, strong) DocumentDirectory *documentDirectory;
@end
@implementation DownloadManager
- (instancetype)init
{
self = ;
if (self) {
self.progress = ;
;
self.settings = ;
self.documentDirectory = ;
}
return self;
}
- (void) dealloc {
;
}
//Download the file from remote server in the document directory as Zip format
- (void) downloadCarContents:(NSArray *)urlArray forContent:(NSArray *)contentArray {
AFHTTPSessionManager *manager = ;
self.progress.totalUnitCount =urlArray.count;
self.progress.completedUnitCount = 0;
for (NSInteger i = 0; i < urlArray.count; i++) {
NSString *destinationPath = ];
NSURLRequest *request = ]];
NSURLSessionTask *task = [manager downloadTaskWithRequest:request progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
return ;
} completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
}];
NSProgress *childProgress = ;
;
NSLog(@"Total downloaded : %f", self.progress.fractionCompleted * 100.0);
;
}
}
@end
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>你忘了实现 <code>observeValueForKeyPath:ofObject:change:context:</code> 方法。</p>
<pre><code>- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary<NSString *, id> *)change
context:(void *)context {
if () {
// process value
}
}
</code></pre></p>
<p style="font-size: 20px;">关于ios - 为多个文件下载添加子进度,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/35249845/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/35249845/
</a>
</p>
页:
[1]