Fetching relations with Core Data
I have two managed objects which have a bidirectional relationship. It is a 'segmentWithDetails' which contains a 'segment' object.
I use
NSEntityDescription *entity = [NSEntityDescription entityForName:@"SegmentWithDetails"
inManagedObjectContext:connectionDetailsService.connectionDetailsContext];
[fetchRequest setEntity:entity];
to get my segmentWith Details. This works ok.
BUT, when I try to retrieve the contained segment, I get nil.
I've tried to get it with
Segment *segment = [segmentWithDetails valueForKeyPath:@"segment"];
and with
Segment *segment = segmentWithDetails.segment;
But this does not work (=nil). I've also tried modifying the NSFetchedResultsController. I added the following:
[fetchRequest setRelationshipKeyPathsForPrefetching:[NSArray arrayW开发者_运维百科ithObject:@"segment"]];
[fetchRequest setIncludesSubentities:YES];
But both of these do not seem to make any difference. From what I understand out of Apple fine documentation, the relationship should just work using faults.
What am I missing here? How do I get to the relationship object 'segment' from 'segmentWithDetails' ??
It should just work like you describe. Are you sure the "segment" object/relation actually exists and contains a real object? Check the data model that this relation isn't optional and check your code that you have actually stored something.
Problem solved. It was not how I read the data, but how I put it in CoreData. The posted code above is indeed correct. Thanx for the response Jaanus.
精彩评论