small medium large xlarge

Back to: All Forums  Core Data
Generic-user-small
04 Nov 2009, 02:34
Jennifer Mitchell (4 posts)

Not directly related to the book, but I hope someone can help.

I’m using the book (and other resources such as Tim Isted’s blog entries) to help me with my app, which creates objects in new windows instead of the tables. I’ve got it working fine for simple objects, but when I try to add an object with a relationship, it complains the entities are in two different contexts. They are, and I know why, but I don’t know how to solve it.

The new window has its own managedObjectContext while I’m creating the new entity. This is so only when I click “Save” does the object actually get added to the main MOC. (Otherwise, it saves after each field loses focus, which is not what I want.) The problem is, the objects in the dropdown box (a list of Categories to add a type to a person in an address book) have to be in the main MOC (since that’s where the Categgory entities are), not in the window MOC. Whenever I select one from the dropdown box, it complains about the different contexts. When I save, the object itself gets saved to the main context (in my method that I explicitly do this in), but I don’t know how to make the relationship do it.

Is there any way I can make the relationship to the entity based on the string of the value? Would a FetchRequest be able to do that for me?

Generic-user-small
04 Nov 2009, 04:59
Jennifer Mitchell (4 posts)

I am attempting to grab the entity object based on the string but it’s not working. When I print the object in the log, the “type” is nil, probably because I’m not binding it correctly. When I try to bind it correctly, I get a “this class is not key value coding-compliant” error message. Hmmm….

Avatarsmall_pragsmall
06 Nov 2009, 03:49
Marcus S. Zarra (284 posts)

Instead of using a different context for each window, which is a rather heavy handed solution, I would recommend you look into using the functionality of the NSUndoManager. You can wrap each window in a NSUndoManager transaction which will then allow you to roll it back if the user hits cancel and commit when the user hits save.

That will allow you to solve this issue cleanly.

Generic-user-small
16 Nov 2009, 07:59
Jennifer Mitchell (4 posts)

Oh gosh, I never saw this reply until now. I don’t think the subscription worked. Thank you Marcus, yes that is actually the direction I am heading with the project. :) I’ve tried a few solutions of the NSUndoManager but haven’t gotten it to work yet. I may return and post here about that. Do you have a specific example or page in your books you can point me to?

Avatarsmall_pragsmall
17 Nov 2009, 17:24
Marcus S. Zarra (284 posts)

Take a look over at the MDN (Mac Developer Network). I did an article on using the UndoManager that may prove helpful.

You must be logged in to comment