I am having a class extending ReactiveWindowController
. As in base class I see this:
// subscribe to listen to window closing
// notification to support (de)activation
NSNotificationCenter
.DefaultCenter
.AddObserver(NSWindow.WillCloseNotification,
_ => deactivated.OnNext(Unit.Default), this.Window);
Hence in my subclass I am writing the callback to remove the observer, as
public partial class SplitViewWindowController : ReactiveWindowController
{
~SplitViewWindowController()
{
Console.WriteLine("Destructor of SplitViewWindowController");
}
public SplitViewWindowController() : base("SplitViewWindow")
{
Console.WriteLine("Constructor of SplitViewWindowController");
this.Deactivated.Take(1).Subscribe(x => {
// NSNotificationCenter.DefaultCenter.RemoveObserver(NSWindow.WillCloseNotification);
// NSNotificationCenter.DefaultCenter.RemoveObserver(this);
//NSNotificationCenter.DefaultCenter.RemoveObserver(Owner);
});
}
But I am lost to find a suitable way to remove the Observer. Or I am doing something wrong here?
Why am I removing the Observer? The answer is this SplitViewController is not dealloc-ed if any observer remains un-registered. I tried with NSWindowController, here if all the observers are removed, the deallocation works and the destructor's logs prints. If I do not remove observer even in case of subclassing from NSWindowController it doesn't call the destructor.
So the fix is to remove the Observer but how?