2010-08-06 79 views
7

這是我的問題。我有這個模型[Event]有多個[Day],在[Event]中有一個叫做days的關係,並且在[Day]中有一個反向關係事件。NSPredicate通過關係而不是屬性過濾數據

現在我想傳遞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?

回答

14

我想通了:

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

感謝把這個了! – Aaronium112 2011-02-01 18:25:40