ios - 设置 FetchedResultsController 中的特殊结果

标签 ios objective-c core-data

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

我正在尝试根据电路之间的关系获取 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 中获取具有更高电路的 Paradas 列表,这不容易解释,但在我的应用程序中,您有两个具有相同 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 - 设置 FetchedResultsController 中的特殊结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22694976/

相关文章:

ios - Prototype Cell 在 ios 7+ 上有空白内容

ios - dismissViewControllerAnimated 没有关闭 ViewController

ios - 传递一个委托(delegate)作为 NSURLSession 的参数

iphone - 如何使用 NSFetchedResultsController 支持像 iPhone 照片应用程序一样管理 UITableView 上的数据

objective-c - 批处理核心数据获取结果

ios - 为什么 nspredicate 不起作用

objective-c - 使用 NSMutableDictionary 优化 for in 循环

ios - UIAlertAction 处理程序未在 iOS 9 上跨 UIWindows 调用

objective-c - 解析 XML 存储到 NSArray

ios - 来回切换时锁定的 ViewController 方向中断