2014-03-27 94 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