small medium large xlarge

Back to: All Forums  Core Data
Photo_2_pragsmall
23 Oct 2009, 06:38
Kurt Arnlund (4 posts)

I’m following the project in the book using the latest version of xcode 3.2.1.
The application template has created an AppDelegate object named “Grokking_Recipes_AppDelegate” with the following ivars:

    NSWindow *window;
    
    NSPersistentStoreCoordinator *persistentStoreCoordinator;
    NSManagedObjectModel *managedObjectModel;
    NSManagedObjectContext *managedObjectContext;

These properties are declared as follows:

@property (nonatomic, retain) IBOutlet NSWindow *window;
@property (nonatomic, retain, readonly) NSPersistentStoreCoordinator *persistentStoreCoordinator;
@property (nonatomic, retain, readonly) NSManagedObjectModel *managedObjectModel;
@property (nonatomic, retain, readonly) NSManagedObjectContext *managedObjectContext;

The problem comes in section 2.6 “Adding the NSArrayController Objects”. Step #5 says to use the Bindings tab of the inspector to bind the ManagedObjectContext to the AppDelegate with the model key path of managedObjectContext.

IB simply won’t let me do this, I believe because the managedObjectContext property is defined as read only in the AppDelegate. There are no managedObjectContext’s that I can bind to unless I am supposed to drag one from the object library.

Thanks for your help.

Photo_2_pragsmall
27 Oct 2009, 04:13
Kurt Arnlund (4 posts)

I’m kind of bummed that there hasn’t been a response to this yet. I bought this book and pdf so I could quickly gain some skills in Core Data but this issue is sending me back to figuring it out from the Apple documentation. So far, not much savings. From what I can tell the latest XCode template makes significant changes to the first steps in creating the Grokking Recipes app example. I can see from the template code it makes different assumptions about what will be created in code and what will be created in the Xib.

Anyway, here is how I got the first example working with the XCode 3.2.1 template:

1) I added a Managed Object Context object into my xib then linked the NSArrayContoller objects to it. This has allowed nearly everything else in the example to be setup like the book. It allowed the app to run but I got errors that say “Cannot perform operation since managed object context has no persistent store coordinator” whenever I’d click the + or - to add/remove a recipe record.

2) To fix the fact that the Managed Object Context didn’t know about the persistent store coordinator I moved around a few things in their - (NSManagedObjectContext *) managedObjectContext function. It now looks like this:

Avatarsmall_pragsmall
26 Oct 2009, 23:56
Marcus S. Zarra (284 posts)

It didn’t get responded to in a couple of days because I was out of town at a conference.

In the book, the instructions are to bind the NSArrayController objects to the AppDelegate’s managedObjectContext. You do not need to drag a managedObjectContext object into the xib because you just need to configure the binding. This is done by selecting the NSArrayController and opening its inspector to the Bindings tab. At the bottom of the bindings tab there is a section for the ManagedObjectContext binding.

!http://img.skitch.com/20091026-8npqydewwwigfwxbhynx1kicba.png

Avatarsmall_pragsmall
26 Oct 2009, 23:59
Marcus S. Zarra (284 posts)

As for your re-write of the -managedObjectContext method, I do not recommend it. It does not gain anything over the code that is provided in the template. There should never be a situation where the moc has been initialized and it does not have a persistent store coordinator. If you are seeing that situation then there is another error in the code somewhere else that you need to track down.

Photo_2_pragsmall
27 Oct 2009, 05:44
Kurt Arnlund (4 posts)

Thanks for that additional picture, I can see now where I went wrong. I was misled by the image right above step #5 showing the “Content Set” bindings for the Recipe Ingredients NSArrayController and I didn’t know about the Parameters section of the Bindings tab, so I kept making bindings in the wrong area of the panel. That change works as I had hoped it would. I’ve rarely played with the bindings panel so a lot of this IB territory is new.

Thanks for your help!

“A picture that shows more detail”: http://skitch.com/kurtarnlund/nr5eq/ibscreensnapz004

Avatarsmall_pragsmall
27 Oct 2009, 17:20
Marcus S. Zarra (284 posts)

There are many areas of Cocoa development that I decided to cover lightly because the Core Data book is meant to be a bit more advanced and focused on a single technology. While I would certainly like to cover all of Interface Builder, bindings, et al. the book would still be in the beta and probably not in print until 2011 :)

If you want to explore bindings and interface builder more I would suggest picking up Daniel Steinberg’s excellent book on general Cocoa development as he covers that aspect of development with far more depth.

Generic-user-small
31 Mar 2010, 17:13
Justin Mrkva (4 posts)

Wow, I was wondering about a problem and thought it would be really nice to be able to contact the author… lo and behold! This was a great help and solved one of the major problems I had, which happened to be the same as Arnlund.

I now have another problem after fixing that one - I can’t add ingredients. Here’s my Ingredients Array Controller bindings: http://drp.ly/ISWt

And my table view column bindings: http://drp.ly/IT11N http://drp.ly/IT2bk http://drp.ly/IT31V

And the “Add” button: http://drp.ly/IT31V

And my data model for good measure: http://drp.ly/IT7j

But I just get a blank table for the ingredients, and the add button has no effect at all. Thoughts?

EDIT: Also, there are no errors/messages from Xcode. It just does nothing when clicking the button.

EDIT 2: Solved! I had to bind the Ingredients array controller to the AppDelegate’s managedObjectContext and ALSO bind it to the Recipes array controller.

Generic-user-small
10 Jun 2010, 14:06
Joshua Tharp (3 posts)

@Justin

Thanks for the follow-up edit. I had the same problem, but with both sections set in IB, it works great.

Duncwa-office_pragsmall
20 Nov 2011, 23:48
Duncan Wallace (4 posts)

If that had read “5. On the the Bindings tab of the inspector, in the Parameters section, bind ManagedObjectContext to the AppDelegate with a model key path of managedObjectContext.”, the three of us would not have had this thread discussion.

Generic-user-small
03 Dec 2011, 14:03
Dave Brennan (6 posts)

Marcus,

I bought your book some time ago but have only now started to try to get to grips with it. As you’ll be aware Apple keep moving things around in Xcode. Do you have any plans to update the book to use the Xcode 4.2 interface? I’m having enough problems understanding Core Data without having to translate all the Xcode instructions to a completely different interface so this would be very useful, although I recognise it would be a big job.

Dave

Avatarsmall_pragsmall
11 Dec 2011, 20:50
Marcus S. Zarra (284 posts)

Yes there are plans to update the book so that it matches Xcode 4. I do not have a schedule at this time.

You must be logged in to comment