Hi Austin
> Thank you Joanna, that was very helpful. So, from your experience,
> mergeChangesFromContextDidSaveNotification: will not pick up adding a new
> object, just editing an existing one? It seems like the real pain point is
> having to re-create the new object in the second managedObjectContext and
> copy all it's properties.
My tutorial is meant to demonstrate several different techniques, to give
people an idea of what is available, especially if they are coming from a
different programming language.
I included the routine for copying the object contents, possibly because I too
came across Tim Isted's blog and, at the time, thought that was what was
necessary to ensure that object properties were populated from the correct MOC.
However, I have just checked and found that you can, indeed, use the same
methodology for both editing and adding a new object. Here is some revised code
that you can use in place of the sheetDidEnd:returnCode:contextInfo: method in
the tutorial.
- (void) sheetDidEnd:(NSWindow *)sheet returnCode:(int)returnCode
contextInfo:(void *)contextInfo
{
// we are only interested in doing something with the edited word
// if the Save button was pressed
if (returnCode == NSOKButton)
{
NSNotificationCenter *dnc = [NSNotificationCenter defaultCenter];
[dnc addObserverForName:NSManagedObjectContextDidSaveNotification
object:managedObjectContext
queue:nil
usingBlock:^(NSNotification *saveNotification)
{
[[sourceListController managedObjectContext]
mergeChangesFromContextDidSaveNotification:saveNotification];
[sourceListController fetch:nil];
}];
NSError *error;
if (![managedObjectContext save:&error])
{
NSLog(@"Error saving edited Word");
}
[dnc removeObserver:self
name:NSManagedObjectContextDidSaveNotification
object:managedObjectContext];
}
else
{
// Cancel button pressed -
// rollback any changes to the temporary context
[managedObjectContext rollback];
}
// close the dialog
[sheet orderOut:self];
// clear temporary object from controller
[wordController setContent:nil];
}
The only real difference here is that, within the callback block, you need to
call [sourceListController fetch:nil] to ensure that the original browsing list
is not just refreshed but that the sort descriptors are honoured to include the
new object.
Regards
Joanna
--
Joanna Carter
Carter Consulting
_______________________________________________
Cocoa-dev mailing list ([email protected])
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com
This email sent to [email protected]