【问题标题】:using a strong NSProgress with downloadtaskwithrequest使用强大的 NSProgress 和 downloadtaskwithrequest
【发布时间】:2015-01-31 14:03:14
【问题描述】:

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

我正在使用一个具有强大 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

【问题讨论】:

    标签: ios automatic-ref-counting afnetworking-2


    【解决方案1】:

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

    [myApiClient downloadFileWithUrl:_url progress:&_progress];
    

    You can find more details from this link

    【讨论】:

    • 如果这是您需要的答案,请将其标记为正确。谢谢
    【解决方案2】:

    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;
    }
    

    【讨论】:

      猜你喜欢
      • 2018-02-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-11
      • 1970-01-01
      相关资源
      最近更新 更多