fuzzygoat
fuzzygoat

Reputation: 26223

retaining when returning?

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];

Upvotes: 2

Views: 87

Answers (2)

Rengers
Rengers

Reputation: 15238

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.

Upvotes: 6

Costique
Costique

Reputation: 23722

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.

Upvotes: 0

Related Questions