duringLifetimeOf
运算符并仅使用一次性保留来处置deinit
,该dint也可以正常工作。
PS:当我不从NSObject
继承时,相同的代码有效。下面的一些示例代码 -
protocol ObservableFeatureSource {
var futureFeatureChanges: Signal<AnyFeature, NoError> { get }
}
extension FeatureManager: ReactiveExtensionsProvider {}
extension Reactive where Base: FeatureManager {
func monitorFeatureChanges<F>(for feature: ApplicationFeature<F>) -> Signal<ApplicationFeature<F>, NoError> {
let observableSources = FeatureManager.shared.sources
.flatMap { ($0 as? ObservableFeatureSource)?.futureFeatureChanges }
return Signal<AnyFeature, NoError>.merge(observableSources)
.filterMap { $0 == feature ? feature : nil }
}
}
final class Example: NSObject {
static let shared = Example()
private override init() {
super.init()
FeatureManager.shared.reactive
.monitorFeatureChanges(for: .someFeature)
.observe(on: UIScheduler())
.take(duringLifetimeOf: self)
.observeValues({ _ in
debugPrint("print something")
})
}
}