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:
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.
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?
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;
}
Code:
-(IBAction)saveBook:(id)sender
{
[[self managedObjectContext] insertObject:[self book]];
}
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?