Last active
November 17, 2018 14:58
-
-
Save ValCanBuild/9e094c2e3f64d050fb4c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// DisposeBagProvider.swift | |
import RxSwift | |
import UIKit | |
/* | |
// Just make a class conform to it like this: | |
class MyClass : DisposeBagProvider { | |
} | |
// And then just use that "disposeBag" variable | |
// when you need a disposeBag. | |
*/ | |
protocol DisposeBagProvider { | |
var disposeBag: DisposeBag {get} | |
} | |
extension DisposeBagProvider where Self: AnyObject { | |
var disposeBag: DisposeBag { | |
get { | |
if let bag = objc_getAssociatedObject(self, &AssociatedKeys.DisposeBagKey) as? DisposeBag { | |
return bag | |
} | |
let bag = DisposeBag() | |
objc_setAssociatedObject(self, &AssociatedKeys.DisposeBagKey, bag, .OBJC_ASSOCIATION_RETAIN_NONATOMIC) | |
return bag | |
} | |
} | |
} | |
private struct AssociatedKeys { | |
static var DisposeBagKey = "dispose_bag_key" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment