2012-03-22 53 views
0

我用下面的代碼我的核心數據圖表來獲取數據的對象ID篩選:NSFetchRequest,

- (void)setupFetchedResultsController 
{ 
    // 1 - Decide what Entity you want 
    NSString *entityName = @"Snag"; // Put your entity name here 
    NSLog(@"Setting up a Fetched Results Controller for the Entity named %@", entityName); 

    // 2 - Request that Entity 
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:entityName]; 

    // 3 - Filter it if you want 
    request.predicate = [NSPredicate predicateWithFormat:@"project.id = %@", projectPassedToController.id]; 

    // 4 - Sort it if you want 
    request.sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"dateTaken" 
                        ascending:YES 
                         selector:@selector(localizedCaseInsensitiveCompare:)]]; 
    // 5 - Fetch it 
    self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request 
                     managedObjectContext:self.managedObjectContext 
                      sectionNameKeyPath:nil 
                        cacheName:nil]; 

不過,我只想要回那個有相同的唯一標識符作爲一個項目這是傳遞給此視圖控制器(使用ProjectPassedToController變量)

我不想過濾project.name,因爲此字段可由用戶編輯。我需要過濾項目的唯一標識符,但我不知道如何做到這一點。

回答

0

通過過濾項目和人之間的關係解決了這個問題。

request.predicate = [NSPredicate predicateWithFormat:@"belongsToProject = %@", _selectedProject];