• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 为多个文件下载添加子进度

[复制链接]
菜鸟教程小白 发表于 2022-12-12 18:27:00 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我正在我的项目中使用 AFNetworking 3.0 下载多个文件。我想显示所有文件的单个下载进度。我将每个文件下载的每个子进度添加到父进度中。但它不起作用,应用程序崩溃了。我收到了错误 -

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '<DownloadManager: 0x7f92e2f6e130>: An -observeValueForKeyPathfObject:change:context: message was received but not handled.
Key path: fractionCompleted

这是我的下载类-

@interface DownloadManager ()

@property (nonatomic, strong) NSProgress *progress;
@property (nonatomic, strong) Settings *settings;
@property (nonatomic, strong) DocumentDirectory *documentDirectory;

@end


@implementation DownloadManager

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.progress = [NSProgress new];
        [self.progress addObserver:self forKeyPath"fractionCompleted" options:NSKeyValueObservingOptionNew context:NULL];
        self.settings = [Settings new];
        self.documentDirectory = [DocumentDirectory new];
    }

    return self;
}

- (void) dealloc {
    [self.progress removeObserver:self forKeyPath"fractionCompleted"];
}


//Download the file from remote server in the document directory as Zip format
- (void) downloadCarContentsNSArray *)urlArray forContentNSArray *)contentArray {

    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];

    self.progress.totalUnitCount =  urlArray.count;
    self.progress.completedUnitCount = 0;

    for (NSInteger i = 0; i < urlArray.count; i++) {

        NSString *destinationPath = [self.documentDirectory getDownloadContentPath:contentArray[i]];
        NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlArray[i]]];

        NSURLSessionTask *task = [manager downloadTaskWithRequest:request progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {

            return [NSURL fileURLWithPath:destinationPath];

        } completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {


        }];

        NSProgress *childProgress = [manager downloadProgressForTask:task];
        [self.progress addChild:childProgress withPendingUnitCount:1];

        NSLog(@"Total downloaded : %f", self.progress.fractionCompleted * 100.0);

        [task resume];
    }
}

@end



Best Answer-推荐答案


你忘了实现 observeValueForKeyPathfObject:change:context: 方法。

- (void)observeValueForKeyPathNSString *)keyPath
                      ofObjectid)object
                        changeNSDictionary<NSString *, id> *)change
                       contextvoid *)context {
    if ([keyPath isEqualToString"fractionCompleted"]) {
        // process value
    }
}

关于ios - 为多个文件下载添加子进度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35249845/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap