Re: Knowing when a NSArrayController is ready
Re: Knowing when a NSArrayController is ready
- Subject: Re: Knowing when a NSArrayController is ready
- From: David LeBer <email@hidden>
- Date: Sat, 24 Jan 2009 13:33:37 -0500
On 24-Jan-09, at 1:11 PM, Dave Fernandes wrote:
It looks like you can set to prepare content AND call
fetchWithRequest:
http://developer.apple.com/DOCUMENTATION/Cocoa/Conceptual/CoreData/Articles/cdBindings.html
"If the "automatically prepares content" flag (see, for example,
setAutomaticallyPreparesContent:) is set for a controller, the
controller's initial content is fetched from its managed object
context using the controller's current fetch predicate. It is
important to note that the controller's fetch is executed as a
delayed operation performed after its managed object context is set
(by nib loading)—this therefore happens after awakeFromNib and
windowControllerDidLoadNib:. This can create a problem if you want
to perform an operation with the contents of an object controller in
either of these methods, since the controller's content is nil. You
can work around this by executing the fetch "manually" with
fetchWithRequest:merge:error:. You pass nil as the fetch request
argument to use the default request, as illustrated in the following
code fragment."
Ah, very ilumminating. Thanks Dave.
I'd read through the bindings documentation, but hadn't seen this.
On Jan 24, 2009, at 12:59 PM, Matt Neuburg wrote:
On Sat, 24 Jan 2009 09:07:59 +0000, "email@hidden"
<email@hidden> said:
On 24 Jan 2009, at 02:43, David LeBer wrote:
This may be a stupid question, but I've had reason to wonder a
number of times.
If I have an NSArrayController in a nib, bound to a MOC, configured
with an Entity name and set to prepare content:
Is there a way to be notified when it has finished loading and
arranging it's objects?
Hi David
Adding an observer on the controller's arrangedObjects property
should
do the trick.
[arrayController addObserver:self forKeyPath:@"arrangedObjects"
options:0 context:(void *)&ControllerContentContext];
In a persistent document based app, for example, the controller's MO
content is not yet available in - (void)windowControllerDidLoadNib:
(NSWindowController *)windowController.
Adding the observer means you can set up any dependent code when the
binding/core data machinery has finished doing its thing.
I'd tried this, but discounted it for some forgotten reason (I hate
those). When I get a chance I'll play with it again. I think the fact
that specifying the NSKeyValueObservingOptionNew,
NSKeyValueObservingOptionOld options didn't return anything for
NSArrayControllers turned me off.
Is that really true? I'm not saying you're wrong; I'm really
asking. When I
posed the same question a couple of years back, I was told that the
solution
is, do *not* "set to prepare content"; instead, load the data
yourself by
calling fetchWithRequest:
[arrayController fetchWithRequest:nil merge:NO error:&error];
When that call returns, the data is ready. I just now tried adding
addObserver: to the mix, just as an experiment, and logging reveals
that I
get two corresponding calls to observeValueForKeyPath:, *before*
fetchWithRequest: has returned. The two calls are quite far apart
(half a
second or so). Well, I sure wouldn't want to run my "dependent
code" half a
second too early. So I'm not convinced that this approach is
entirely safe,
at least not without some further modification... m.
--
matt neuburg, phd = email@hidden, <http://www.tidbits.com/matt/>
A fool + a tool + an autorelease pool = cool!
One of the 2007 MacTech Top 25: <http://tinyurl.com/2rh4pf>
AppleScript: the Definitive Guide - Second Edition!
<http://www.amazon.com/gp/product/0596102119>
;david
--
David LeBer
Codeferous Software
'co-def-er-ous' adj. Literally 'code-bearing'
site: http://codeferous.com
blog: http://davidleber.net
profile: http://www.linkedin.com/in/davidleber
twitter: http://twitter.com/rebeld
--
Toronto Area Cocoa / WebObjects developers group:
http://tacow.org
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden