I have two objects set in the following relationship:
News <->> Link
Link contains two attributes where one is a URL and the other is some associated text. I initially created News and populate it with information:
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"];
Then inside a for loop, I create a Link:
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];
After the for loop is done, I put the set of Link of I created to the News object:
[newsData setValue:links forKey:@"links"];
When I’m fetching for the data, I can fetch the information for News just fine, but when I fetch for the Link, I get nothing back:
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"]];
}
Can anyone please give their insight on why I’m getting back 0 objects when I try to access my related objects?
I solved it with the following code: