我在以下关系中设置了两个对象:
新闻 <->> 链接
链接包含两个属性,一个是 URL,另一个是一些关联的文本。我最初创建了 News 并用信息填充它:
NSManagedObject *newsData = [NSEntityDescription
insertNewObjectForEntityForName:@"News"
inManagedObjectContext:context];
[newsData setValue:[object objectForKey:@"username"] forKey:@"username"];
[newsData setValue:message forKey:@"content"];
[newsData setValue:[object objectForKey:@"when"] forKey:@"date"];
[newsData setValue:imgUrl forKey:@"img"];
NSMutableSet *links = [launchTicker mutableSetValueForKey:@"links"];
然后在 for 循环中,我创建了一个链接:
NSManagedObject *linkInfo = [NSEntityDescription
insertNewObjectForEntityForName:@"Link"
inManagedObjectContext:context];
[linkInfo setValue:[object objectForKey:@"id"] forKey:@"id"];
[linkInfo setValue:[[item firstChild] content] forKey:@"text"];
[linkInfo setValue:[[item attributes] valueForKey:@"href"] forKey:@"url"];
[links addObject:linkInfo];
在 for 循环完成后,我将创建的一组 Link 放入 News 对象:
[newsData setValue:links forKey:@"links"];
当我获取数据时,我可以很好地获取新闻的信息,但是当我获取链接时,我什么也得不到:
for (NSManagedObject *info in fetchedObjects) {
[fetchRequest setReturnsObjectsAsFaults:NO];
[fetchRequest setRelationshipKeyPathsForPrefetching:[NSArray arrayWithObjects:@"links", nil]];
NSMutableArray *url = [[NSMutableArray alloc] init];
NSMutableArray *text = [[NSMutableArray alloc] init];
NSSet* links = [info valueForKey:@"links"];
NSArray *arrayLinks = [links allObjects];
for(NSManagedObject *link in arrayLinks) {
[url addObject:[link valueForKeyPath:@"url"]];
[text addObject:[link valueForKeyPath:@"text"]];
}
当我尝试访问我的相关对象时,任何人都可以就我为什么返回 0 个对象提供他们的见解吗?