Reputation: 441
I have the folowing Core Data model:
That is, ach ItemCategory belongs to a project
(reverse: categories
), and can optionally belong to a parent ItemCategory (category
, reverse subcategories
) — please ignore the rest.
Thing is, apparently I can't set both the project and the parent category for an ItemCategory. Every time I set one relationship the other relationship is set to nil. I've tried every combination of delete rule, but nothing seems to work.
Is there a fundamental Core Data restriction I'm not aware, or am I doing something wrong?
Upvotes: 0
Views: 251
Reputation: 6715
You can have multiple to-many relationships between two entities. The important thing is that each relationship has its own inverse. Let's say we have entity A and B and we want two to-many relationships between them. You can do that.
We'll start out with
A <<----->> B
Your A entity will have a relationship which we'll call bs with an inverse which we'll call as. So far everything is normal.
Nothing prevents you from adding another relationship, though:
<<----->>
A B
<<----->>
For the 2nd relationship, we'll name the relationship from A to B as otherBs and its inverse for otherAs.
Now A has two relationships to B, namely bs and otherBs. And the same goes for the relationships the other way.
Upvotes: 1
Reputation: 31016
I'll vote for "doing something wrong". :) I just tried the simplest form of this with a Project, an ItemCategory, no attributes, no other entities, their own relationships...and it doesn't show that symptom. This...
Project *p = [NSEntityDescription insertNewObjectForEntityForName:@"Project" inManagedObjectContext:self.managedObjectContext];
ItemCategory *c1 = [NSEntityDescription insertNewObjectForEntityForName:@"ItemCategory" inManagedObjectContext:self.managedObjectContext];
ItemCategory *c2 = [NSEntityDescription insertNewObjectForEntityForName:@"ItemCategory" inManagedObjectContext:self.managedObjectContext];
c1.category = c2;
c1.project = p;
...gives...
(lldb) po c1.category (ItemCategory *) $2 = 0x06b98bd0 <ItemCategory: 0x6b98bd0> (entity: ItemCategory; id: 0x6b98ee0 <x-coredata:///ItemCategory/tB2F1219F-4FA5-48DA-871E-D9F9DC8E33E34> ; data: {
category = nil;
project = nil;
subcategories = (
"0x6b82280 <x-coredata:///ItemCategory/tB2F1219F-4FA5-48DA-871E-D9F9DC8E33E33>"
); })
(lldb) po c1.project (Project *) $3 = 0x06b97a20 <Project: 0x6b97a20> (entity: Project; id: 0x6b97a70 <x-coredata:///Project/tB2F1219F-4FA5-48DA-871E-D9F9DC8E33E32> ; data: {
categories = (
"0x6b82280 <x-coredata:///ItemCategory/tB2F1219F-4FA5-48DA-871E-D9F9DC8E33E33>"
); })
Unfortunately, this doesn't shed any light on what may be wrong but it doesn't seem inherently a restriction.
Upvotes: 0