views:

58

answers:

2

Should I be retaining the responseData that I am returning

// METHOD
-(NSData *)dataFromTurbine:(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];

gary

A: 

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.

Costique
+6  A: 

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.

Rengers
Much appreciated Rengers, I was getting mixed up, thanks again.
fuzzygoat