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!