Reputation: 49097
When using the NSURLConnection can I release it at the end of the method where it is instantiated?
NSURLConnection* connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
...
...
[connection release];
Then it continue with the asynchronous process, or do I need to retain it?
Upvotes: 0
Views: 134
Reputation: 8546
The best is to use connectionWithRequest:Delegate: so that it's autoreleased.
You should however also keep a pointer to your NSURLConnection until the connection end to have the opportunity to nil the delegate. Otherwise in the case where your connection does not return but your delegate gets deallocated before being called by the connection, it will crash. ( you'll get the error: message sent to deallocated instance)
So the best is to keep a pointer to your NSURLConnection and set the pointer to nil when the connection return. So in ConnectionDidFinishLoading
connection = nil;
Now if the delegate gets deallocated you can put something in the dealloc method or in viewDidUnload if your delegate is a viewController
if (connection != nil) connection.delegate = nil;
Upvotes: 0
Reputation: 1137
I really give you the advice to use convenience allocators whenever possible. The objects you get back are autoreleased meaning (in easy words) you won't have to worry about manual releasing or retaining. To answer your question with more then a rule of thumb: use
connectionWithRequest:delegate:
and you're good to go.
Upvotes: 0
Reputation: 89
You should hold the connection by "assign", the reason is that: you need to manage the connection for example:cancel it.
Upvotes: 0
Reputation: 11839
You need to release connection object when you are done.
Release this in NSURLConnection
delegate methods -
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
Upvotes: 2