Search code examples
iosautomatic-ref-countingafnetworking-2

using a strong NSProgress with downloadtaskwithrequest


I'm facing a strong vs. autorelease problem :

I'm using an object which have a strong NSProgress to manage some file download. For downloading, i'm using downloadtaskwithrequest from AFNetworking. My problem is that this method take a NSProgress * __autoreleasing * which is not compatible with my strong NSProgress :

This is my object owning its NSProgress :

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

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

This is the SessionManager dealing with the download :

-(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)
        { ... }];
}

This is the error concerning the line progress:progress :

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

Solution

  • It's downloadTaskWithRequest who initialize the NSProgress object, so I cannot give it directly a NSProgress which is property of my object, i had to create another NSProgress object, and to update my property when needed :

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