small medium large xlarge

Back to: All Forums  Core Data
19 Sep 2009, 14:39
Massimo Cafaro (4 posts)

I am trying to copy a Core Data object, and striving to follow the example code you provide in Chapter 9, Section 9.5. After reading the chapter there is still something unclear. Will you please better explain why in the -associateObject: parent: method you treat objects belonging to the RecipeIngredient entity differently from all of the others? Is the “name” property the only reason for doing this?

What I need to fully understand, is how to behave correctly in the general case. In my Entity there are some relationships, some of them are to-one, others are to-many. Some entities have the “name” properties”, many don’t. I just have a few relationships in total. Should I simply treat each one as you treat RecipeIngredient in the -associateObject: parent: method?

In practice, quoting the book it is not clear why “If we are working with a RecipeIngredient then we know to just blindly create a new object. However, for any other object, we need to first check to see if it already exists in the NSManagedObjectContext.”

20 Sep 2009, 15:01
Marcus S. Zarra (284 posts)

We treat the RecipeIngredient special because in that specific situation we know going into the method that it must be created new as that is the only way it will be entered. Because we know that a RecipeIngredient will only ever have one Recipe and further we know that a copy operation (in this application) occurs top down from the Recipe that we therefore must create a new one. All of the objects below RecipeIngredient do not fall under that rule as Ingredient objects, etc. can be shared between Recipes. RecipeIngredients are not shared and therefore we can skip that step.

You must be logged in to comment