简体   繁体   中英

Do I need to release object using delegate?

I have a task to do without ARC. Previously I didn't use it (started studying ios development recently). I have a class that represents http request, it conforms to NSURLSessionDownloadDelegate protocol. And also I have following code:

-(void)executeWithRelativeUrl:(NSString *)relativeUrl andSuccessBlock: (void(^) (NSData*))successBlock {
    NSURL *url = [[NSURL alloc] initWithString:relativeUrl relativeToURL:self.baseUrl];
    [self setSuccessBlock:successBlock];
    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
    NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
    NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfiguration delegate:self delegateQueue:nil];
    NSURLSessionDownloadTask *downloadTask = [session downloadTaskWithRequest:request];
    [downloadTask resume];

    [request release];
    [url release];
}

that creates url session and starts download task. I'm dealing with task results in following method:

-(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location {
    NSData *data = [NSData dataWithContentsOfURL:location];

    dispatch_async(dispatch_get_main_queue(), ^{
        self.successBlock(data);
    });

}

Now the question is: do I need to release session, download task and location url in the end of the last method? Or it will be done for me? I'm asking this because I created it in the first method (except for url), and as I understand the one who is responsible for releasing the object is also me. Thanks!

The Golden Rule is very simple. Did you say alloc or copy or retain ? No? Then you don't need to say release (and you must not do so).

(You need to release the url and the request for that reason, and you are doing so. So memory management is now complete.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM