I am converting an existing data model which was previously stored in core data in XML, so I try to learn I am doing the ropes as far as possible as the core data is clearly one of the techniques that is never going anywhere, so I can also "learn this right". Take a core data model with two entities, for example:
- person
- Food < One-to-many relationship with
person 2 food
- Favored Foods (1-to-many)
- Foods with hate (1-to-many)
(Both person and food are both sub-nsmainage objects I can say Arth should have:
person * Fred = [[individual light] init]; NSArray * fredsFavorites = fred.favoriteFoods;
Easy data
I am reading through the documentation for core data, and I can not find the correct way to get this NSArray
NSFetchRequest , because which relationships I have I want to get it from the object, can not define it.
NSFetchRequest * request = [[NSFetchRequest alloc] init]; [Request Set Entity: [NSTTTDCification Unit FERN: @ManagedObjectContext @ "Food": [Fred Managed Object Contex]]]; [RequestIncludesSubentities: No]; NSArray * FredsFavoriteAndHatFoods = [[Fred managed object contaxes] executeFetchRequest: request error: zero]; << Code>
Returns all Food items stored in both
custom food and
hatedFoods . How can I divide them? Definitely a simple explanation, but now I am not able to understand this concept so that it can be explained in core data chunks, thus my Google search is useless.
The easiest way to achieve this is to directly access
NSSet :
NSArray * fredsFavorites = [fred. Favorite all objects];
(How I have received the
NSSr to
NSArray as a result)
Alternatively, if you establish untrue relationships (which you should do), then you can use a fetch request like this:
NSFetchRequest * Request = [[NSFetchRequest alloc] init]; [Request Set Entity: [NSTTTDCification Unit FERN: @ManagedObjectContext @ "Food": [Fred Managed Object Contex]]]; [Acknowledgment of the request: [NSPredicate predicateWithFormat: @ "Anyone with this effacephrat ==% @", Fred]]; NSArray * fredsFavoriteFoods = [[Fred managed object contaxes] executeFetchRequest: request error: zero];
It assumes that
peoplewiththeAsFavorite
has an inverse relationship with the favorite unless I am not reading your example wrong,
Favorite Food There should be many relationships in fact, because one person can have many favorite foods, and there may be a favorite person with such people in the food.
(Note that I did not test it, so
NSPredicate can not be 100% correct)
- Food < One-to-many relationship with
Comments
Post a Comment