7

这是我的问题。我有这个模型,一个[Event]有多个[Day],[Event]中有一个叫做days的关系,[Day]有一个反向关系事件。

现在我想传递 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?

4

1 回答 1

14

我想到了:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF IN %@", self.currentEvent.days];
于 2010-08-06T05:37:18.310 回答