I’m moving on to the existing data model that was previously stored in XML for Core Data, so I try to learn the ropes as best as possible. Core Data, obviously, is one of those technologies that aren’t going anywhere in the near future, so I could also “learn it right”.
Take, for example, a Core Data model with two objects:
A person has 2 one-to-many relationships with Food :
- favoriteFoods (1-to-many)
- hatedFoods (1-to-many)
(Both human and food are subclasses of NSManagedObject.)
In the previous Person data model, two NSArray instance variables were NSArray . If I wanted my favorite dishes, I could call:
Person *fred = [[Person alloc] init]; NSArray *fredsFavorites = fred.favoriteFoods;
Easy compression.
I am reading the documentation for Core Data, and I cannot find the right way to get this NSArray based on NSFetchRequest , because I cannot determine with what relation I want to get the objects.
NSFetchRequest *request = [[NSFetchRequest alloc] init]; [request setEntity:[NSEntityDescription entityForName:@"Food" inManagedObjectContext:[fred managedObjectContext]]]; [request setIncludesSubentities:NO]; NSArray *fredsFavoriteAndHatedFoods = [[fred managedObjectContext] executeFetchRequest:request error:nil];
This returns all Products items stored in both favoriteFoods and hatedFoods . How can I separate them? Of course, there is a simple explanation, but I do not understand this concept well enough to explain it in the Core Data jargon, so my Google searches are fruitless.
source share