我遇到从一对多关系中获取对象的问题。 我有两个实体“精选”和“事件”,特色对象包含一个特色事件的属性,这是一个多人关系,一个“特色”项目到很多“事件”。
现在我需要使用NSFetchedResultsController返回“featuredEvents”关系中的事件。我正在使用以下代码:
NSFetchedResultsController *fetchedFeaturedEvents;
NSFetchRequest *fetchRequest;
NSError *error = nil;
fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"SDMFeaturedEvent"];
[fetchRequest setSortDescriptors:@[]];
fetchedFeaturedEvents = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:theManagedObjectContext sectionNameKeyPath:nil cacheName:nil];
[fetchedFeaturedEvents performFetch:&error];
if (error) {
NSLog(@"Unable to perform fetch.");
NSLog(@"%@, %@", error, error.localizedDescription);
}
return fetchedFeaturedEvents;
这很棒我返回特色事件,但我也想添加一个谓词,只返回“featuredEvents”属性中的事件。所以问题是如何创建谓词?
我基本上需要所有“事件”在“特色”对象的featuredEvents属性中进行提取,这可能吗?
答案 0 :(得分:0)
对于那些遇到类似问题的人,我最终使用了这种方法:
NSFetchRequest *fetchRequest;
NSError *error = nil;
NSArray *fetchedObjects;
NSPredicate *soldOutPredicate;
NSPredicate *criteriaMatchPredicate;
NSCompoundPredicate *fullPredicate;
NSSortDescriptor *usesDescriptor = [[NSSortDescriptor alloc] initWithKey:@"eventDate" ascending:YES];
fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"FeaturedEvent"];
[fetchRequest setSortDescriptors:@[]];
fetchedObjects = [theManagedObjectContext executeFetchRequest:fetchRequest error:&error];
if (error) {
NSLog(@"Unable to perform fetch.");
NSLog(@"%@, %@", error, error.localizedDescription);
}
if ([[NSSDMUserDetails sharedManager]showsCriteraMatchEvents]) {
if ([[NSSDMUserDetails sharedManager]isUserMale]) {
criteriaMatchPredicate = [NSPredicate predicateWithFormat:@"(eventAgeMaleMin <= %@) AND (eventAgeMaleMax >= %@)",[[[NSSUserDetails sharedManager]userAge]stringValue],[[[NSUserDetails sharedManager]userAge]stringValue]];
}
else{
criteriaMatchPredicate = [NSPredicate predicateWithFormat:@"(eventAgeFemaleMin <= %@) AND (eventAgeFemaleMax >= %@)",[[[NSUserDetails sharedManager]userAge]stringValue],[[[NSUserDetails sharedManager]userAge]stringValue]];
}
fullPredicate = [NSCompoundPredicate andPredicateWithSubpredicates:@[criteriaMatchPredicate]];
}
if (![[NSSDMUserDetails sharedManager]showsSoldOutEvents]) {
if ([[NSSDMUserDetails sharedManager]isUserMale]) {
soldOutPredicate = [NSPredicate predicateWithFormat:@"eventPlacesMale == %@ || eventPlacesMale != %@",@"in stock",@"0"];
}
else{
soldOutPredicate = [NSPredicate predicateWithFormat:@"eventPlacesFemale == %@ || eventPlacesFemale != %@",@"in stock",@"0"];
}
fullPredicate = [NSCompoundPredicate andPredicateWithSubpredicates:@[soldOutPredicate]];
}
if (soldOutPredicate && criteriaMatchPredicate) {
fullPredicate = [NSCompoundPredicate andPredicateWithSubpredicates:@[soldOutPredicate,criteriaMatchPredicate]];
}
if (fetchedObjects && fetchedObjects.count > 0) {
SDMFeaturedEvent *feat = [fetchedObjects objectAtIndex:0];
NSLog(@"All featured events %@",feat.featuredEvents);
if (fullPredicate) {
return [[feat.featuredEvents.allObjects filteredArrayUsingPredicate:fullPredicate]sortedArrayUsingDescriptors:@[usesDescriptor]];
}
else if (![[NSSDMUserDetails sharedManager]showsCriteraMatchEvents]){
return [feat.featuredEvents.allObjects sortedArrayUsingDescriptors:@[usesDescriptor]];
}
else{
return [NSArray new];
}
}
return [NSArray new];
诀窍是从featuresEvent“featuredEvents”属性返回一系列事件,然后您可以运行filteredArrayUsingPredicate和sortedArrayUsingDescriptor方法,以根据您的要求对数组中的事件对象进行排序。虽然这不是返回NSFetchedResultsController,但这适用于过滤。