views:

30

answers:

0

I have a Core Data object, card, which has a child relationship relatedCards, which references other card objects. I am trying to get Core Data to give me an NSArray of the relatedCards' ObjectID values only—not the other property values for the cards. I know that one option is to run a fetch request such as:

Card *myCard = <something>
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Card" inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];
[fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"any self.relatedCard = %@", myCard]];
[fetchRequest setResultType:NSManagedObjectIDResultType];
NSArray *results = [managedObjectContext executeFetchRequest:fetchRequest error:&error];

However, I want to get this data for a whole bunch of cards, not for a specific one. What is the best way to get all of the Card objects, with a bunch of properties, and also the relationship but only with the Object IDs and none of the other information?

My current code looks like this:

NSMutableDictionary *cards = [[NSMutableDictionary alloc] initWithCapacity:0];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Card" inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];
[fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"group = %@", group]];
[fetchRequest setPropertiesToFetch:[NSArray arrayWithObjects:@"value1", @"value2", @"value3", nil]];
[fetchRequest setRelationshipKeyPathsForPrefetching:[NSArray arrayWithObjects:@"relatedCards", nil]];
[fetchRequest setReturnsObjectsAsFaults:NO];
[fetchRequest setIncludesSubentities:YES];
NSArray *allCards = [managedObjectContext executeFetchRequest:fetchRequest error:&error];

The problem with this is that it loads too much information, and it bogs down the phone. But if I don't prefetch the relatedCards, when I try to get the relatedCards I hit a fault.

Any suggestions? If possible I would like to try to tell Core Data to only give me the ObjectIDs and nothing else.