0

First: I really hope I placed the question at the right place so please correct me if I am on the wrong track..!

I have a window to display data that is updated in intervals in a table view. This data is managed by a Controller object which is the data source of the tableview. The WindowController has a controller property that is bound to the Controller object in IB:

@implementation WindowController

- (id) initWithWindowNibName:(NSString *)windowNibName {
    self = [super initWithWindowNibName:windowNibName];
    if (self) {
        _controller = [[Controller alloc] init];
    }
    return self;
}

- (void) windowDidLoad {
...

Additionally, I have a WindowHandler class that has a static reference to the 'WindowController' with class methods to open and close the window:

static WindowController * windowController = nil;

@implementation WindowHandler

+ (void) initialize {
    if (self == [WindowHandler class]) {
        if (!windowController) {
            windowController = [[WindowController alloc] initWithWindowNibName:kWindowNibKey];
        }
    }
}

+ (void) showWindow:(id)sender {
    [windowController showWindow:sender];
}

...

Because I want to monitor the data that is to be displayed in the window even before the window is opened the first time, I do a [WindowHandler initialize] in my AppDelegate's applicationDidFinishLaunching:. This initiates the WindowController and therefore the Controller object. The Controller starts monitoring the data after launching which is my desired outcome.

The problem arises when the window is opened for he first time: a new instance of the Controller object is initiated, obviously when the window nib is loaded. From this time two Controller objects are running and observing the data which is not only unnecessary but also increases CPU usage. Further more, I suspect this to infrequently lead to crashs (sometimes I do get exceptions saying 'insertRowsAtIndexes:withRowAnimation: can not happen while updating visible rows!').

I guess I miss something in the concept of object properties or maybe in the concept of bindings. Is there something I do wrong? Or is there a good way of doing what I attempt to do? Any help appreciated! Thanks in advance!

Simon
  • 577
  • 3
  • 9
  • What would trigger initWithWindowNibName twice for your WindowController class? "When the window is opened for the first time" -- you aren't doing this programattically? It's mainmenu.xib or something? You need to not init two windowControllers if you don't need two WCs. – stevesliva Jun 18 '14 at 02:02
  • @stevesliva: Thanks for your reply! I don't no what is triggering initWithWindowNibName twice, this is what's puzzling me. The window can be opened from various points in my app (from a menu, by pressing a key shortcut or by pressing a button in another window). To open the window I do `[WindowController openWindow:nil]` (I edited the code so you can see the method). – Simon Jun 18 '14 at 07:36
  • Set breakpoints on `initWithWindowNibName` and `initialize` and watch the call stack on each invocation. I think you should avoid explicitly calling `initialize`. I can't explain why it would init your static variable twice, though. I was thinking the runtime was nesting your explicit initialize in its automagic initialize, but I can't see how you'd get two... `dispatch_once` might help (see modern singleton code patterns), but understanding why it's done twice would be better. – stevesliva Jun 18 '14 at 18:31
  • @stevesliva: **SOLVED** `initialize` was called twice after beeing called from `applicationDidFinishLaunching:`. Looking at the object id I could still tell that only one `Controller` object was initiated while a second would be initiated when the window was opened for the first time. Following your next hint in my `AppDelegate` I changed the statement from `[WindowHandler initialize]` to `[WindowController closeWindow:nil]`,`initialize` was only called once and the running `Controller` object was the same after opening the window. I still don't fully understand why.. But thank you very much! – Simon Jun 19 '14 at 00:16
  • @stevesliva: Sorry, I was wrong, but now I'm totally puzzled. Actually, neither `initialize` nor `initWithWindowNibName:` are called twice as determined by breakpoints (--> set to ignore once). But still two `Controller` objects are initiated, both after app finished launching. In the meantime I created a `DataMonitor` class to that other objects can register as observers. This way I can see that two objects are initialzed, but I can't even tell which one will be the one that is the data source of my tableview. I have no clue what's going on.. – Simon Jun 19 '14 at 01:18

0 Answers0