PDA

View Full Version : Direction on using Core Data and Data modeling




mbabauer
May 24, 2006, 09:07 PM
I am new to XCode, Cocoa, and the lot. I have read through some books, Cocoa Programming for Mac OS X 2nd Edition and Beginning Mac OS X Programming are the main two, but I have yet to really see a book that covers 10.4 and Core Data along with the Data Modeler.

I attempted today to write a simple app, a recipe manager with categories, using the Data Modeler to model out the entities. I then dropped an NSBrowser on my window, added a NSTreeController, and set the NSTreeController to "Entity",named Category, and set the Content binding of the NSBrowser to the NSTreeController. I also added two buttons, binding one to canInsertChild and the other to canRemoveChild, and connected them to insertChild and removeChild of NSTreeController.

So, I start it up, and I see an empty NSBrowser, but nother seems to work. What am I doing wrong?

The next question is: Are there any good books out that cover Core Data. I am also looking for some "Best Practices" books. Primarily I want books covering 10.4, but it looks like there aren't any yet.

Oh, forgot to mention I am a Sun Certified Developer in Java 2, so my background is Java/C++/C, but I am trying to learn Cocoa and Objective-C.



Gruffalo
May 25, 2006, 03:14 PM
Don't know if it might help at all, but have you seen the ADC video tutorial (http://developer.apple.com/cocoa/coredatatutorial/index.html)?

GeeYouEye
May 25, 2006, 07:14 PM
Unfortunately, it's my experience that NSTreeController is buggy, if not outright broken. Check the Cocoa-dev mailing list at Apple for more info on that, as well as possible workarounds.

mbabauer
May 25, 2006, 07:24 PM
Unfortunately, it's my experience that NSTreeController is buggy, if not outright broken.

I actually tried using the NSArrayController first, but apparently the NSBrowser only works with trees, which I thought was weird. How can you have multiple root nodes then? As long as the data structure underneith the array was tree-like, I don't see the problem.

Don't know if it might help at all, but have you seen the ADC video tutorial?

I am definately going to watch tonight. One of the problems is, unlike most of the other learning challenges I have had in my CS career, I am doing this one solo. Non of my friends have even heard of Cocoa or Objective-C. This means all the trial and error crap is on me. I have gotten one of my CS buddies to finally buy a Mac, but he is more interested in Ruby on Rails at the moment, which is something I already tackled.

I would really like to see a book on the subject. The Cocoa programming book by Aaron Hillegass is awesome...maybe he will release an updated 3rd edition?

mbabauer
May 25, 2006, 09:12 PM
Ok, I took a step back, restarted a new project, and tried to do a table-based app like the demo vs. the NSBrowser-based one like I had.

The one difference is that instead of doing a "Cocoa Data Doc App", I did just the "Cocoa Data App", the thinking being that I really don't want multiple collections of recipies, just one "master" one.

I created the Data Model no sweat. I dropped an NSArrayController on and set it to "Entity" of name "Recipe" (this DOES exist in my model). Then I added an NSTable, and set the bindings of the table column to the match the attributes on my Recipe entity. Finally I add two buttons, a '+' and a '-', and connect them to the 'add' and 'remove' of the NSArrayController, respectively.

At this point, the app compiles are runs, it just doesn't do anything.

Finally, I bind the managedObjectContext to the "File's Owner" managedObjectContext Model Path. This is where things fail. Upon build and run, I get an NSRaiseError from NSObject(NSKeyValueCoding) valueForUndefinedKey.

This "seems" to me to be comming from the table trying to get the Recipe values for a key that doesn't exist, but I double checked the bindings and they all are right.

Am I reading this error right? What should I look to first?

caveman_uk
May 26, 2006, 12:23 PM
Unfortunately, it's my experience that NSTreeController is buggy, if not outright broken. Check the Cocoa-dev mailing list at Apple for more info on that, as well as possible workarounds.
Also check out Wil Shipley's post here (http://wilshipley.com/blog/2006/04/pimp-my-code-part-10-whining-about.html) where he bemoans NSTreeControllers brokenness and actually offers some code to overcome some of the issues.

Icewind
May 30, 2006, 09:12 AM
Hey,

I seem to be playing around with Core Data the same as you. I too have a Wintel background in Java/C++ etc. and have just started developing Cocoa apps on OSX. The ADC "Building a Sample Core Data Application" Videos are superb. I learned a lot from them.

However, have you tried just Option-Dragging your Entity right from the Data Modeller into an empty window in Interface Builder? Believe it or not. It auto-generates a fully-working interface, complete with all the bindings to your entities attributes. It also includes a Search box, a Table View and the correct UI controls based on the data type of your attributes, i.e. a checkbox for BOOL etc. After that, just "Build & Go"... magic :)

If you haven't ried this yet, give it a bash. Even if it's not exactly what you are attempting to achieve, at leats you can study the bindings to the data model in Interface Builder and see exactly how its done.

mbabauer
May 31, 2006, 11:57 AM
However, have you tried just Option-Dragging your Entity right from the Data Modeller into an empty window in Interface Builder?

I did try this, and you are right its pretty damn neat. It wasn't the interface I was really hoping for, however, so I quickly ditched it. Didn't think about studying how it did the bindings, though...thats a good point.

mbabauer
Jun 1, 2006, 08:58 AM
Ok, now I am utterly confussed...

I again started over, mapped out my Data Model, then tried Option-dragging the model Entity I wanted over and choose "Multiple". I get a nice pretty set of basic components...cool.

Next I run it, no errors except I get some complaint about not having the managedObjectContext set when I hit the add button. No problem...I thought this should be set anyway. I goto the ArrayController, and set the binding for managedObjectContext to File's Owner managedObjectContext. Once again, it blows chunks when I run it.

Am I understanding this wrong? What am I missing?

Icewind
Jun 1, 2006, 09:14 AM
What Project Type did you select when you created your new project? Was it the Core Data Application (Persistent Document) or the other one? Excuse my vagueness. I've not got my Mac here in the office. Crappy ThinkPad I'm on right now.

My project instantiated the ManagedObjectContext no problem. I think I chose the Persistent Document type project, but suppose it depends on what you're trying to achieve.

This is a good little Tutorial (if you haven't seen it yet)
http://developer.apple.com/documentation/Cocoa/Conceptual/NSPersistentDocumentTutorial/00_Introduction/chapter_1_section_1.html

I won't be able to check exactly what I done till I get back home from London tomorrow night.

mbabauer
Jun 1, 2006, 10:07 AM
I just picked "Core Data Application", not the "Core Data Document Application", since I don't want muliple documents, just one single, always there document (if that makes any sense). Sort of like how iTunes works. You don't have multiple libraries of songs, just one "main" library that opens each time you open it. Maybe I am going about it all wrong.

Icewind
Jun 2, 2006, 04:52 AM
Yeah, That must have been what I selected to start my Project, as I only have a single-document structure (like iTunes). When I launch my Application, the UI opens up and all my previously entered records are displayed. There is no Save/Save As... option with my application. I can add/remove records, then just exit the app. When I reopen, the data is in the correct state, i.e. exactly how it was when I closed it.

Hope you get it sussed out.