PDA

View Full Version : Creating Core Data objects outside of Managed Object Context




Joanna Carter
Jul 8, 2009, 10:40 AM
Hi folks

I am trying to create a simple example Core Data application.

I have two entities in the model: an Author that has one attribute of Name, and a Book that has a title attribute and a relationship to an Author.

The main form contains a TableView that displays the Books and that only allows adding of new books by clicking on a button that opens a separate editing form for the new Book.

I am creating the new Book in the controller for the editing form:


-(BookEditingController*)initWithManagedObjectContext:(NSManagedObjectContext*)inMoc
{
self = [super initWithWindowNibName:@"BookEditor"];

[self setManagedObjectContext:inMoc];

NSEntityDescription *bookEntity = [NSEntityDescription entityForName:@"Book" inManagedObjectContext:[self managedObjectContext]];

NSManagedObject *newBook = [[NSManagedObject alloc] initWithEntity:bookEntity insertIntoManagedObjectContext:nil];

[self setBook:newBook];

return self;
}I pass nil as the managed object context when calling initWithEntitity:insertIntoManagedObjectContext because this avoids the new book appearing in the TableView before the Book's roperties have been populated.


-(IBAction)saveBook:(id)sender
{
[[self managedObjectContext] insertObject:[self book]];
}My idea was to use the insertObject method to respond to the "Save" button on the editing form and to insert the Book into the context.

I am using a PopupButton to select the Author but, unfortunately, although the Name attribute of the Book is deemed to be valid, the Author doesn't get set and therefore, unless I edit the Book again after inserting it into context, the Author relationship remains invalid and the document can't be saved.

I am guessing this is because the Author relationship on the Book is bidirectional and thus, because the Book is outside of the context, the Author doesn't get updated (by adding the Book to its Books relationship) as it would normally inside a context.

Does anyone have any ideas how to edit a single object before having it become visible in the list to which it will eventually be added?



lucasgladding
Jul 8, 2009, 10:46 PM
Sorry for a short answer - my day has been pretty long and bed is calling. :)

You may want to check out the Core Data tutorials and examples available on the iPhone site. I remember something about creating a separate context for new objects and merging contexts when the new object is populated and validated. I typically only work with the main context for the document class, so unfortunately I can't say anything more than that offhand. There are only a few Core Data tutorials and examples on the iPhone Dev site, so the document should be quick to find. I'll take a look in the morning for the specific item if the response isn't here by then already.

Kind regards

Luke Gladding

lucasgladding
Jul 8, 2009, 10:49 PM
Couldn't resist. Check out RootViewController.m in the CoreDataBooks sample code. That should give you the answer. It looks like you just create a new context with the same persistent store and merge the changes when you are done editing. You'll need to modify some of the code if you're working on desktop apps, but most of the code should be usable.

Joanna Carter
Jul 9, 2009, 06:50 AM
Hi Luke

Thank you so much for finding that, I now have a working app :D

After nearly 14 years of developing and consulting, first in Delphi, and then in C#, I have found it really hard to get to grips with the Cocoa libraries. The number one problem seems to be copious documentation but very scarce examples. Do you know if there are any books that really address this lack of examples, or does it still have to be a matter of digging and digging through the Apple-provided examples to find domething that suits the problem?

I find it odd that the example you found was under the iPhone section, and yet there doesn't seem to be the equivalent for general OS X programming.

Never mind, I will keep on digging; I shall be presenting a session, on the work I am now doing, at the UK Developers Group in September. Here's hoping I can make everything work :rolleyes:

petron
Jul 9, 2009, 07:02 AM
Hi Joanna,
I as well started to use the Cocoa/Mac after lots of years left behind the keyboard and discovered the same problems as you did.

I bought a following book
"Cocoa Programming for Mac OS X" written by Aaron Hillegass

The book is a very good stuff, explaining the philosofy behaind the cocoa and it let you through some examples. It even provide some insight view on how to use the Apple reference documentation in order to achive the goal.
I went through the half of the book and it is really fun.

GL
/petron

lucasgladding
Jul 9, 2009, 09:24 AM
There is actually quite a bit of information available, but it can be difficult to find. www.cocoadev.com has quite a bit of information and is hosted by Steven Frank from Panic. That was one of my regular sites when I started writing for the Mac. Even stackoverflow.com can be a good source of information for Mac-specific questions, though I don't visit there often enough to comment on general coverage. www.theocacao.com doesn't get updated as often as it was in the past, but it is also worth watching.

Pragmatic Programmers has released some technology-specific books recently including Core Data and Core Animation. I own both of those, but haven't read much of either yet. I often only visit them when I encounter problems that I can't solve through the documentation. They are probably worth the money if you are looking for examples.

Core Data is something to watch, as its release on the iPhone will probably get quite a bit of attention over the coming months. Though many complain about the resources Apple commits to the iPhone, I think the OS will benefit the Mac development community greatly in the long term. I suspect AppKit will see many changes over the next few Mac OS releases, probably bringing the frameworks much closer to what we see in UIKit. Core Data included, developing on the iPhone OS gives a peek at what is coming in 10.6 and beyond IMHO. It's not uncommon to see notes in the Core Data classes stating something is available in iPhone OS 3.0 and Mac OS X 10.6.

Kind regards

Luke Gladding