Re: KVO and inspectors in document-based apps -- best practice?
Re: KVO and inspectors in document-based apps -- best practice?
- Subject: Re: KVO and inspectors in document-based apps -- best practice?
- From: Steven Kramer <email@hidden>
- Date: Fri, 1 Apr 2005 10:49:59 +0200
Op 31-mrt-05 om 19:22 heeft John Brownlow het volgende geschreven:
I've seen this question asked before but not answered so I hope
someone will step up.
I have a document-based app which has several inspector windows which
are independent of any particular document. The window controllers for
these windows are owned and operated by the application's delegate.
They update themselves to reflect whatever the current document is.
I do this by (in IB) binding UI elements and NSArrayControllers to the
respective window controller's -currentDocument method, implemented
like this:
@implementation GLInspectorController
- (NSDocument *)currentDocument
{
return [[NSDocumentController sharedDocumentController]
currentDocument];
}
@end
However, when I close the document, I get an error to the effect that
the document is being deallocated while there are still KVO observers
registered.
As I see it there are two options... either
1. Have the document explicitly check in and out with the application
delegate and have all the inspector windows bind to an iVar in the
application delegate. I think this would work but I would rather rely
on the sharedDocumentManager to judge what is the current document. I
don't see why the document itself should have to worry about it.
2. In the document's dealloc method, unbind all registered observers
before [super dealloc]. Problem is, I don't know how to do this.
NSKeyValueObserving declares a method that seems appropriate but the
documentation is entirely opaque... it just says...
- (void *)observationInfo
Returns a pointer that identifies information about all of the
observers that are registered with the receiver, the options that were
used at registration-time, etc. The default implementation of this
method retrieves the information from a global dictionary keyed by the
receiver’s pointers.
So what is the best practice for binding inspector windows to the
current document??
I think the best practice is to bind to NSApp, with a keypath like
"mainWindow.windowController.document" (check the documentation). This
is more robust but also dog-slow in many cases when switching the
active application. Or at least in the case of XCode on my G3
Powerbook, where alt-tabbing to and from XCode can take seconds. In my
application, I have added a special 'document' key in NSApp that
doesn't change when the application is (de)activated. The difference in
response time is very noticeable. If you can do the same, you will make
my Powerbook that much happier ;-)
Regards,
Steven Kramer
--
email@hidden
http://sprintteam.com/
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden