A queue to keep and reusing objects.
A reuse queue is a way to quickly reuse objects when object allocation and initialization is time-consuming. This reuse queue is inspired after UITableView's for reusing cells, headers and footers.
You can either clone this repository and add the files in the ACReuseQueue
directory to your project; or use CocoaPods.
Add a pod entry to your Podfile:
pod 'ACReuseQueue', '~> 0.0.1'
Install the pod(s) by running:
pod install
You can then use the default queue with:
ACReuseQueue *myQueue = [ACReuseQueue defaultQueue];
or allocate and initialize your own queue.
To reuse an object, call dequeueReusableObjectWithIdentifier:
[myQueue dequeueReusableObjectWithIdentifier:@"myIdentifier"];
When you are done using an object, return it to the queue with enqueueReusableObject:
[myQueue enqueueReusableObject:myObject];
To be reused, an object must conform to the ACReusableObject
and must implement the reuseIdentifier
@property (nonatomic, copy) NSString *reuseIdentifier;
If an object may implement the prepareForReuse
method, it will be called before the object is returned by dequeueReusableObjectWithIdentifier:
You can register a class or a nib for a given identifier with registerClass:forObjectReuseIdentifier:
or registerNib:forObjectReuseIdentifier:
. It will automatically create an object for you if no object is available when dequeueing:
[[ACReuseQueue defaultQueue] registerClass:ACButton.class forObjectReuseIdentifier:@"button"];
or
[[ACReuseQueue defaultQueue] registerNibWithName:NSStringFromClass(ACButton.class)
bundle:nil
forObjectReuseIdentifier:@"button"];
If you have appledoc installed, you can generate the documentation by running the corresponding target.
The ACReuseQueueDemo
target contains an example application to compare the performance with and without the reuse queue. You should preferably run the app on an actual device.
The demo app has a page view controller, where each page contains about 200 buttons (I know the example is a little bit contrived but it serves to expose the performance difference). Swipe both fast and slowly to appreciate the difference.
Here are results of my tests on a ipad mini (note: your performance may vary):
a) without a reuse queue:
- a page is rendered in approximately 0.2 seconds
b) with a reuse queue:
- the first and second page are rendered in approximately 0.2 seconds as well, BUT
- the next pages are rendered in 0.015 seconds (because it can reuse the buttons)