+-----------+           +-----------+
   | -Parada-  |           | -Autobus- |
   +-----------+           +-----------+
   |  nombre   |           | circuito  |
  +-------------+         +-------------+
  |Relationships|         |Relationships|
  +-------------+         +-------------+
   |  byParada |<--------> |  parada   |
   +-----------+           +-----------+


我正在尝试根据Circuitos之间的关系获取Paradas列表。

例如:

 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列表,具体取决于您在第一个选择的Parada Picker,第二个仅显示具有较高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);
    }


}

最佳答案

您的谓词设置错误。
格式化应在谓词构造级别进行:

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


其中currentCircuitoAsNSNumber是当前选择的“ circuito”,其类型为NSNumber

关于ios - setupFetchedResultsController中的特殊结果,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22694976/

10-09 02:20