Reputation: 748
I have an object that encapsulates an asynchronous NSURL request. When should I release it?
StrangersRequest *request = [[StrangersRequest alloc] init];
request.strangersListener = strangersListener;
[request send];
Analyze in XCode says I should release it immediately - but that's not right because it has to handle the URLRequest callback.
What's an appropriate pattern for releasing objects that handle asynchronous events?
I'm guessing I should release when the request completes (connectionDidFinishLoading
). If so, how can I stop Analyze from complaining about it?
Upvotes: 2
Views: 109
Reputation: 3386
You could follow the NSThread
model and have the object retain itself until its asynchronous task has completed.
Upvotes: 2
Reputation: 28242
One solution is to store it in an ivar instead of a local variable.
Upvotes: 0
Reputation: 58087
You should maintain a reference to it in the class that uses it and then trash it immediately. Then, your class can remove the reference when it's done. Something like this:
StrangersRequest *request = [[StrangersRequest alloc] init];
request.strangersListener = strangersListener;
[request send];
self.strangersRequest = request;
[request release];
Upvotes: 4