在NSObject子类的初始化程序中设置时,ReactiveSwift观察器不起作用

时间:2018-03-02 20:01:22

标签: reactive-cocoa reactive-swift

当我在一个类的初始化器中设置一个观察者,从NSObject进行子类化时,无论出于何种原因它都没有被调用,我在工作时遇到了一个奇怪的问题。我尝试删除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")
            })
    }
}

0 个答案:

没有答案