开发者

retaining when returning?

开发者 https://www.devze.com 2022-12-24 07:25 出处:网络
Should I be retaining the responseData that I am returning // METHOD -(NSData *)da开发者_C百科taFromTurbine:(NSString *)pathToURL {

Should I be retaining the responseData that I am returning

// METHOD
-(NSData *)da开发者_C百科taFromTurbine:(NSString *)pathToURL {

    NSURL *url = [[NSURL alloc] initWithString:pathToURL];
    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
    NSHTTPURLResponse *response = nil;
    NSError *error = nil;
    NSData *responseData = [NSURLConnection sendSynchronousRequest:request 
                                                 returningResponse:&response 
                                                             error:&error];

    [request release];
    [url release];
    return responseData;
}

.

// CALLED
NSData *newData = dataFromTurbine(kTurbineDataPath);
[doSomething newData];


Since the method name doesn't start with init, new or copy, dataFromTurbine should return an autoreleased instance of NSData. (Which is already true now for responseData)

The calling method then has ownership, and should retain if needed.


In a word, no.

The NSData object you get from NSURLConnection is autoreleased, so you should retain/release it only if you need to keep it. Otherwise, it will be automatically released for you at the next pass of the run loop.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号