开发者

Usage of autorelease pools for fetch method

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 m开发者_运维技巧uch 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


You must never release something you are still using; or that you are returning to someone for their use. Autorelease is perfectly valid method on the iPhone, and the autoreleasepool is drained at then end of each iteration of the run loop (so basically, for every event).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜