简体   繁体   English

我需要使用委托释放对象吗?

[英]Do I need to release object using delegate?

I have a task to do without ARC. 没有ARC,我有一个任务要做。 Previously I didn't use it (started studying ios development recently). 以前我没有使用过(最近开始研究ios开发)。 I have a class that represents http request, it conforms to NSURLSessionDownloadDelegate protocol. 我有一个代表http请求的类,它符合NSURLSessionDownloadDelegate协议。 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. 创建url会话并开始下载任务。 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? 现在的问题是:我是否需要在最后一种方法的末尾释放会话,下载任务和位置url? 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. 我问这个问题是因为我是在第一种方法中创建的(URL除外),据我了解,负责释放对象的人也是我。 Thanks! 谢谢!

The Golden Rule is very simple. 黄金法则很简单。 Did you say alloc or copy or retain ? 您是说alloc还是copyretain No? 没有? Then you don't need to say release (and you must not do so). 然后,您无需说release (也不必这样做)。

(You need to release the url and the request for that reason, and you are doing so. So memory management is now complete.) (由于这个原因,您需要释放urlrequest ,并且您正在这样做。因此,内存管理现已完成。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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