Oganesson is part of the E-sites iOS Suite.
A small swift helper class for using an ObjectPool
Compatible with:
- Swift 5
- Xcode 11
Installation
CocoaPods
pod 'Oganesson'
SwiftPM
.package(url: "https://github.com/e-sites/Oganesson", .branch("master"))
Usage
Init
class SomeView: UIView, ObjectPoolCompatible {
required convenience init() {
self.init(frame: CGRect(x: 0, y: 0, width: 100, height: 100)
}
}
var objectPool: ObjectPool<SomeView>!
override func viewDidLoad() {
super.viewDidLoad()
objectPool = ObjectPool<SomeView>(size: 20, policy: .dynamic) { obj in
obj.backgroundColor = UIColor.red
}
objectPool.onAcquire { [view] obj in
DispatchQueue.main.async {
view.addSubview(obj)
}
}
objectPool.onRelease { obj in
DispatchQueue.main.async {
// It's safe to remove the object from its superview,
// since `ObjectPool` will keep its (memory) retained.
obj.removeFromSuperview()
}
}
}
Get an object from the pool:
do {
let object = try objectPool.acquire()
object.backgroundColor = UIColor.orange
} catch let error {
print("Error acquiring object: \(error)")
}
Done using the object:
objectPool.release(object)
Policies
dynamic
: If the pool is drained, fill up the pool with +1static
: The pool size is fixed. If the pool is drained, throwError.drained