简体   繁体   中英

Object pooling in Objective-C

Is there a nice way to do this in Objective-C, or do I have to write my own tedious logic?

I'm creating and destroying a little of little state objects per frame in an iPhone game. It would be nice if I could just reuse objects from a pool.

The Sparrow framework for the iPhone contains a class called "SPPoolObject". The framework uses it internally for helper objects that are used extremely often, like points, rectangles or matrices.

If you inherit from SPPoolObject, the 'dealloc' method does not really delete it; instead, the memory will be reused for the next alloc'ed object.

It's quite a simple class - you can easily use it for your projects. It does not have any dependency, so you can easily grab it out of the Sparrow framework ;)

Neither Cocoa nor Objective-C does anything particularly helpful for object pools. They don't do anything to stop you either, but you'll basically have to DIY.

I think that the table View has some sort of pooling mechanism for the different TableCell

something like that :

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
                    cellId];
    if (cell == nil) 
    {
        cell = [[[UITableViewCell alloc]
                initWithStyle:UITableViewCellStyleDefault
                reuseIdentifier:CheckMarkCellIdentifier] autorelease];
    }

Not sure if there is something more generic to use.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM