views:

71

answers:

3

I have a core data model with 2 entities: Collection and Image. There's a one to many relationship between Colletion and Image, reflected by an 'images' accessor on the Collection side and a 'collection' accessor on the Image side.

There's an additional relationship called keyImage, which is a 1:1 relationship between Collection and Image. One of the images in the collection is the key image and I've modeled that by creating an additional 1:1 relationship. This one has an accessor called keyImage in Collection and isKeyImageFor in Image.

I can work with this model mostly fine, there is however one thing that doesn't work.

// Add an image as the key image.
coll.keyImage = keyImage;

// Add the image to the collection
[coll addImagesObject:keyImage];

Both of these lines work independently. However, if I do both (the image should both be in the collection and assigned as the keyImage) then keyImage ends up being null after saving the data. It's as if the 1:N relationship nullifies the 1:1 relationship, even though they use separate keys and accessors.

Any idea how I can get this to work?

A: 

how does the source files look like? have you written code on your own?

Mr Q.C.
A: 

"keyImage" variable name collision?

coll.keyImage = keyImageObject;

[coll addImagesObject:keyImageObject];

dev is at home
A: 

I don't think you can make Core Data do this. You've created two contradictory relationships.

Maybe add a property 'isKeyImage', to the Image entity, and then you'd be able to pull out the single image with that property set.

Or, keep the key image separate from Images. Unfortunately I think you'd need a separate KeyImage entity defined in Core Data, with it's own relationship from Collection. I'd be happy to learn that you can make multiple links between the same entities.

Graham Perks