Usage of autorelease pools for fetch method

Posted by Matthias on Stack Overflow See other posts from Stack Overflow or by Matthias
Published on 2010-04-29T13:24:40Z Indexed on 2010/04/29 13:27 UTC
Read the original article Hit count: 214

Filed under:
|

Hi,

I'm a little bit confused regarding the autorelease pools when programming for the iPhone. I've read a lot and the oppionions seem to me from "Do-NOT-use" to "No problem to use".

My specific problem is, I would like to have a class which encapsulates the SQLite3 Access, so I have for example the following method:

-(User*)fetchUserWithId:(NSInteger)userId

Now, within this method a SQL query is done and a new user object is created with the data from the database and then returned. Within this DB Access class I don't need this object anymore, so I can do a release, but since the calling method needs it, I would do an autorelease, wouldn't I?

So, is it okay to use autorelease here oder would it gain too much memory, if this method is called quite frequently? Some websites say, that the autorelease pool is released first at the end of the application, some say, at every event (e.g. user touches something).

If I should not use autorelease, how can I make sure, that the object is released correctly? Can I do a release in the fetch method and hope, that the object is still there until the calling method can do a retain?

Thanks for your help!

Regards Matthias

© Stack Overflow or respective owner

Related posts about iphone

Related posts about objective-c