Coder Social home page Coder Social logo

acreusequeue's Introduction

ACReuseQueue

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.

Build Status Cocoapods Cocoapods

Install

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

Usage

Reuse queue

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];

Reusable object

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:

Registering a class or a nib

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"];

Documentation

If you have appledoc installed, you can generate the documentation by running the corresponding target.

Demo

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)

acreusequeue's People

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.