CocoaPods trunk is moving to be read-only. Read more on the blog, there are 19 months to go.
TestsTested | ✓ |
LangLanguage | SwiftSwift |
License | MIT |
ReleasedLast Release | Mar 2016 |
SPMSupports SPM | ✗ |
Maintained by Christopher Luu.
RecyclingCenter
is a simple manager that handles dequeuing and enqueuing reused objects. It works similar to how UITableView
and UICollectionView
dequeue their cells. Instead of registering a class, you register an initHandler
closure, which returns your Recyclable
class.
Recyclable
objectsYou will need to have a RecyclingCenter
for each kind of Recyclable
class you want to use. Simply initialize a center and register an initHandler
for your reuseIdentifier
:
let recyclingCenter = RecyclingCenter<RecyclableView>()
recyclingCenter.registerInitHandler({ (_) in return RecyclableView(color: .redColor()) }, forReuseIdentifier: ViewController.redReuseIdentifier)
Later when you want to dequeue a Recyclable
object by calling:
let redView = recyclingCenter.dequeueObjectWithReuseIdentifier(ViewController.redReuseIdentifier, context: nil)
When you want to recycle a view, simply enqueue it:
recyclingCenter.enqueueObject(redView, withReuseIdentifier: ViewController.redReuseIdentifier)
Take a look at the Example project for a more concrete example. Try playing around with the +Red
and +Blue
buttons and you should see logs when the RecyclingCenter
has to create the objects. Remove them using the -Red
and -Blue
buttons and when you re-add new views you should see that the views are being recycled. If you simulate a memory warning, you should see the recycled objects get flushed out, thus having to create new ones.