Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Jul 8, 2009, 10:40 AM   #1
Joanna Carter
macrumors newbie
 
Join Date: Jul 2008
Location: Southport, England
Creating Core Data objects outside of Managed Object Context

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:

Code:
-(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.

Code:
-(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?
Joanna Carter is offline   0 Reply With Quote
Old Jul 8, 2009, 10:46 PM   #2
lucasgladding
macrumors 6502
 
Join Date: Feb 2007
Location: Waterloo, Ontario
 
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
__________________
Lucas Gladding
Development/Interface Design/Illustration
www.cashdepositapp.com | www.ledgerapp.com
www.dribbble.com/lucasgladding
lucasgladding is offline   0 Reply With Quote
Old Jul 8, 2009, 10:49 PM   #3
lucasgladding
macrumors 6502
 
Join Date: Feb 2007
Location: Waterloo, Ontario
 
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.
__________________
Lucas Gladding
Development/Interface Design/Illustration
www.cashdepositapp.com | www.ledgerapp.com
www.dribbble.com/lucasgladding

Last edited by lucasgladding; Jul 8, 2009 at 10:54 PM.
lucasgladding is offline   0 Reply With Quote
Old Jul 9, 2009, 06:50 AM   #4
Joanna Carter
Thread Starter
macrumors newbie
 
Join Date: Jul 2008
Location: Southport, England
Hi Luke

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

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
Joanna Carter is offline   0 Reply With Quote
Old Jul 9, 2009, 07:02 AM   #5
petron
macrumors member
 
Join Date: May 2009
Location: Malmo, Sweden
Send a message via ICQ to petron
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
petron is offline   0 Reply With Quote
Old Jul 9, 2009, 09:24 AM   #6
lucasgladding
macrumors 6502
 
Join Date: Feb 2007
Location: Waterloo, Ontario
 
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
__________________
Lucas Gladding
Development/Interface Design/Illustration
www.cashdepositapp.com | www.ledgerapp.com
www.dribbble.com/lucasgladding
lucasgladding is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Core Data do you have to pull out an object before you can update it? whitedragon101 iPhone/iPad Programming 3 Feb 23, 2014 05:13 PM
How do I retrieve managed objects whose dates fall within one week of a given date? moonman239 iPhone/iPad Programming 5 Jan 16, 2014 05:16 PM
How do I access a child class's reference to a managed object? moonman239 iPhone/iPad Programming 2 Nov 6, 2013 05:40 PM
Using two two managed object contexts for two threads RookieAppler iPhone/iPad Programming 3 Jan 30, 2013 06:55 PM
Creating Objects on the Fly tdewitt274 iPhone/iPad Programming 5 Nov 4, 2012 02:50 PM

Forum Jump

All times are GMT -5. The time now is 04:59 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC