使用强大的 NSProgress 和 downloadtaskwithrequest

using a strong NSProgress with downloadtaskwithrequest

我面临着一个强大的自动释放问题:

我正在使用一个具有强大 NSProgress 的对象来管理一些文件下载。 对于下载,我使用的是来自 AFNetworking 的 downloadtaskwithrequest。 我的问题是此方法采用 NSProgress * __autoreleasing * 与我强大的 NSProgress 不兼容:

这是我拥有 NSProgress 的对象:

@interface MyDocument ()
@property(nonatomic, strong) NSProgress *progress;
@end

@implementation MyDocument ()
-(void)download
{
    [myApiClient downloadFileWithUrl:_url progress:_progress]
}
@end

这是处理下载的 SessionManager :

-(void)downloadFileFromUrl:(NSString*)url progress:(NSProgress * __strong *)progress
{
    NSURLSessionDownloadTask *downloadTask = [self downloadTaskWithRequest:request 
        progress:progress 
        destination:^NSURL *(NSURL *targetPath, NSURLResponse *response)
        { ... }
        completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error)
        { ... }];
}

这是关于行 progress:progress :

的错误
Passing address of non-local object to __autoreleasing parameter for write-back

您需要将指针传递给 NSProgress 对象,而不是将对象作为参数传递。 ** 表示您必须将指针传递给指向现有对象的指针。

[myApiClient downloadFileWithUrl:_url progress:&_progress];

You can find more details from this link

downloadTaskWithRequest 初始化了 NSProgress 对象,所以我不能直接给它一个 NSProgress,它是我对象的 属性,我不得不创建另一个 NSProgress 对象,并更新我的 属性 需要时:

-(void)downloadFileFromUrl:(NSString*)url progress:(NSProgress * __strong *)progress
{
    NSProgress *localProgress = nil;
    NSURLSessionDownloadTask *downloadTask = [self downloadTaskWithRequest:request 
    progress:localProgress 
    destination:^NSURL *(NSURL *targetPath, NSURLResponse *response)
    { ... }
    completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error)
    { ... }];

    // Update my property here :
    *progress = localProgress;
}