NSDictionaryResultType不提取关系中的对象 - 核心数据

时间:2016-01-05 05:57:51

标签: ios objective-c iphone core-data

从核心数据中提取对象时,我将NSFetchRequest结果类型设置为NSDictionaryResultType。设置NSDictionaryResultType时,关系对象不在结果字典中。这段代码可能存在什么问题。如果NSDictionaryResultType不起作用,我将如何获取关系对象?

// get manageObjectContext
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context = [appDelegate managedObjectContext];

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"SomeName"
                                          inManagedObjectContext:context];
[fetchRequest setEntity:entity];
fetchRequest.relationshipKeyPathsForPrefetching = [NSArray arrayWithObjects:@"anEntity", nil];
[fetchRequest setResultType:NSDictionaryResultType];

fetchRequest.predicate = [NSPredicate predicateWithFormat:@"%K IN %@", @"anId", anArray];

NSError *error;
NSArray *existingTopics = [context executeFetchRequest:fetchRequest error:&error];
fetchRequest = nil;

谢谢!

1 个答案:

答案 0 :(得分:0)

您需要设置属性propertiesToFetch。查看当前找到的NSFetchRequest NSMutableArray *mutableResults = [results mutableCopy]; for (NSUInteger i = 0; i < mutableResults.count; i++) { NSDictionary *result = mutableResults[i]; // Do a similar fetch for the related objects, returning dictionary // representations. Repeat as necessary for each 1toMany relationship. NSArray *objectsFor1toManyRelationship = [self fetchRelationShipFor:result[@"identifier"]]; NSMutableDictionary *mutableResult = [result mutableCopy]; mutableResult[@"1toManyRelationship"] = objectsFor1toManyRelationship; mutableResults[i] = [mutableResult copy]; } 的课程参考。

注意:此属性不适用于一对多(或多对多)关系。

对于此类关系,必须手动添加相关对象。

<强>的伪代码:

Spoon.screenshot(context,fileName)