NSPredicate通过关系而不是属性过滤数据

时间:2010-08-06 03:44:23

标签: iphone core-data nspredicate relationship

这是我的问题。我有这个模型一个[事件]有多个[日],在[事件]中有一个称为天的关系,并且在[日]中有一个反向关系事件。

现在我想要传递Event对象,并使用NSFetchedResultsController获取所有日子,并在表中提供所有日期。这是我的代码:

// Create the fetch request for the entity.
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
// Edit the entity name as appropriate.
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Day" inManagedObjectContext:managedObjectContext];

//set predicate  ->  ??
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"event = %@", self.currentEvent];
[fetchRequest setPredicate:predicate];

//set entity
[fetchRequest setEntity:entity];

// Set the batch size to a suitable number.
[fetchRequest setFetchBatchSize:20];

// Edit the sort key as appropriate.
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"date" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];

[fetchRequest setSortDescriptors:sortDescriptors];

// Edit the section name key path and cache name if appropriate.
// nil for section name key path means "no sections".
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest 
                                                                                            managedObjectContext:managedObjectContext 
                                                                                              sectionNameKeyPath:nil
                                                                                                       cacheName:nil];

不知何故,NSFetchedResultsController实例将始终为不同的事件返回相同的内容。 我该如何修复NSPredicate?

1 个答案:

答案 0 :(得分:14)

我明白了:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF IN %@", self.currentEvent.days];