views:

170

answers:

2

What's the best way to know when my array controller that is bound to my core data store is done loading its content?

I've got some methods (i.e. awakeFromNib) that rely on the array controller having its content, but most of the time the array controller doesn't have its content yet when awakeFromNib is being called on that object.

I want to delay my actions until I know the controller has all of its content.

A: 

See Theocacao for a discussion about this problem

cocoafan
I disagree with using delayed messages, just because there's usually a better place to put that logic (and for the reasons argued about in the comments to that article).
Marc Charbonneau
actually, I looked here at the KVO notices for NSArrayController ( http://developer.apple.com/documentation/Cocoa/Conceptual/CocoaBindings/Articles/ControllerKey-ValueObservingCompliance.html#//apple_ref/doc/uid/TP40002493-DontLinkElementID_3 ) . By adding observers to several just to see the timing, it seems "content" is triggered when it's done loading from the store
ctshryock
+2  A: 

Moving that code to -windowDidLoad is usually a safe bet, it's called after the window is fully loaded compared to -awakeFromNib where you can potentially run into problems because the order on which it's called on all the objects in your nib is not defined.

In general it's a good idea to make a mental note of all the initialization you're doing, what parts need a UI to work correctly, what parts can be delayed until the user performs an action, and so on. For example, it's good to delay tasks like doing a Core Data fetch until the last minute, in case you ever have a window that's not opened until the user requests it. On the other hand, sometimes you'll be working with an object like an outline view that needs its data source to be pre-populated for persistence methods to work.

Once you know what you're doing and in what order it needs to be done, you can choose some combination of init, awakeFromNib, yyyWillLoad, or xxxDidLoad and you'll take care of a lot of bugs like this before they have a chance to cause trouble.

Marc Charbonneau
@ctshryock: you should really consider this answer. My post is only an answer to your question. However, this answer describes what you should do in a real application.
cocoafan
advice taken, thanks to both of you!
ctshryock