2014-03-27 23 views
0
+-----------+   +-----------+ 
    | -Parada- |   | -Autobus- | 
    +-----------+   +-----------+ 
    | nombre |   | circuito | 
    +-------------+   +-------------+ 
    |Relationships|   |Relationships| 
    +-------------+   +-------------+ 
    | byParada |<--------> | parada | 
    +-----------+   +-----------+ 

我試圖根據circuitos之間的關係來獲取Paradas列表。setupFetchedResultsController中的特殊結果

對於防爆:

Autobus 1 has Circuito=1 and Parada=Town 
Autobus 2 has Circuito=2 and Parada=City 
Autobus 3 has Circuito=3 and Parada=Coast 
Autobus 4 has Circuito=4 and Parada=Park 

所以我試圖讓具有來自定義具有較高的電路帕拉達斯的列表,這是不容易解釋,但在我的應用程序,你有兩個選擇器具有相同Parada的列表,取決於您在第一個選取器中選擇的Parada,第二個將只顯示具有更高Circuito的Parada。

例如,如果您在第一個選擇器中選擇了「城市」,則第二個選擇器將讓您選擇「海岸」或「公園」。

這裏是我試圖修復代碼:

- (void)setupFetchedResultsController 
{ 
    // 0 - Ensure you have a MOC 
    if (!self.managedObjectContext) { 
     NSLog(@"RolePickerTVCell wasn't given a Managed Object Context ... so it's going to go get one itself!"); 
     RCAppDelegate *ad = [[UIApplication sharedApplication] delegate]; 
     self.managedObjectContext = ad.managedObjectContext; 
    } 

    // 1 - Decide what Entity you want 
    NSString *entityName = @"Parada"; // Put your entity name here 
    NSLog(@"RolePickerTVCell is 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 
    // Here's the problem: 

    NSString *predString = [NSString stringWithFormat:@"ANY byParada.circuito > '%@'", self.fetchedResultsController]; 
    request.predicate = [NSPredicate predicateWithFormat:predString]; 

    // It shows nothing... 
    NSLog(@"list of Paradas: %@", self.fetchedResultsController); 



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

    NSLog(@"The following roles were fetched for the Picker by ORIGENPickerTVCell:"); 
    for (Parada *fetchedOrigen in [self.fetchedResultsController fetchedObjects]) { 
     NSLog(@"List of Paradas: %@", fetchedOrigen.nombre); 
    } 


} 

回答

2

你的斷言設置是錯誤的。
格式化應該發生在謂語結構層次:

request.predicate = [NSPredicate predicateWithFormat:@"byParada.circuito > %@",currentCircuitoAsNSNumber]; 

哪裏currentCircuitoAsNSNumber是當前選定的「CIRCUITO」,併爲NSNumber類型。

+0

問題是currentCircuitoAsNSNumber必須是自動的,如果我只知道nombre,我怎麼能得到? – Rodrigo

+0

你解釋過你選擇了「城市」,這意味着你可以訪問:'相關'Autobus'對象的'byParada.cicuito',或者你可能給出了你的數據模型的錯誤表示... –

+0

我可以只選擇Para​​da,但我不知道如何將Parada(Town,City ...)的名稱轉換爲當前的CircuitoAsNSNumber,然後我可以取回它。 – Rodrigo