0

i hit a weird issue while working when I set up a observer in the initializer of a class, subclassing from NSObject and for whatever reason it's not getting called. I tried removing duringLifetimeOf operator and retaining with a disposable only to dispose in deinit, that dint work either.

PS: The same code works when I dont subclass from NSObject. Some example code below -

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")
            })
    }
}
Manoj
  • 953
  • 2
  • 8
  • 24
  • Can you confirm that your `init` is definitely being called? – jjoelson Mar 02 '18 at 20:12
  • @jjoelson yes, i can confirm it's called. – Manoj Mar 02 '18 at 20:30
  • Try the `logEvents` operator to see if it is maybe disposed immediately... – MeXx Mar 04 '18 at 17:20
  • @MeXx Thanks for the suggestion, using `logEvents` I can see it's getting disposed too soon. I was suspecting the same and hence earlier I tried explicitly retaining w/ a `Disposable` and then disposing that in `deinit`. That did not work either, so I suspect is it something w/ the lifetime of `NSObject`. Is it different than the non-`NSObject` types? – Manoj Mar 04 '18 at 20:17
  • https://github.com/ReactiveCocoa/ReactiveCocoa/blob/master/ReactiveCocoa/NSObject%2BLifetime.swift – Manoj Mar 05 '18 at 07:25
  • @Manoj, the fact that it didn't work with a normal `Disposable` should mean that the issue is unrelated to `NSObject+Lifetime`, right? – jjoelson Mar 05 '18 at 13:54
  • Maybe try removing `.take(duringLifetimeOf: self)` again and putting a break point in `deinit` to see if the object is getting cleaned up earlier than you expect. – jjoelson Mar 05 '18 at 13:59
  • @jjoelson as I mentioned earlier, I had tried that too. That's not happening either :( – Manoj Mar 05 '18 at 18:51
  • Well my last idea is if `logEvents` is showing early disposal than add `on(disposed:)` and stick a break point in the block and see if the stack trace can tell you anything about what is initiating the disposal. – jjoelson Mar 05 '18 at 18:56
  • @Manoj you mentioned that you tried manual disposal in `deinit` but have you checked at which point `deinit` is actually called? What @jjoelson suggested was that maybe the whole `Example` instance is cleaned up much earlier than you expect – MeXx Mar 06 '18 at 08:49
  • @MeXx `Example` instance is not getting cleaned up yet. – Manoj Mar 06 '18 at 16:57
  • I also wanted to see how notification works, so tried w/ `NotificationCenter.default.reactive.notifications(forName:...` - which is working as expected. – Manoj Mar 06 '18 at 17:02

0 Answers0