如何为 CoreData 多对多关系编写 NSPredicate

Posted

技术标签:

【中文标题】如何为 CoreData 多对多关系编写 NSPredicate【英文标题】:How to write an NSPredicate For CoreData to-many relationship 【发布时间】:2011-01-30 20:15:03 【问题描述】:

我在导航层次结构中有 2 个级别的 UITableViews。第一页是UITableView,它使用NSFetchedResultsController 显示Equipment 的所有实例,效果很好。在第二页上,我想取回 Equipment 的特定部分的所有 Note 实体,以便我可以在 tableView 中显示它们。

如何写NSPredicate 只返回相关注释?

- (NSFetchedResultsController *)fetchedResultsController 

    if (_fetchedResultsController != nil) 
        return _fetchedResultsController;
    

    //Init Fetch Request
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];

    //Entity the NSFetchedResultsController will be managing
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Note" 
                                              inManagedObjectContext:self.myContext];
    [fetchRequest setEntity:entity];

    //Sort Descriptors
    NSSortDescriptor *sortDescriptor1 = [[NSSortDescriptor alloc] initWithKey:@"noteTime" 
                                                                    ascending:YES];//Primary Sort
    NSSortDescriptor *sortDescriptor2 = [[NSSortDescriptor alloc] initWithKey:@"noteText" 
                                                                    ascending:YES];//Secondary Sort
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor1, 
                                                                sortDescriptor2, 
                                                                nil];

    //Set Sort Descriptors
    [fetchRequest setSortDescriptors:sortDescriptors];

    // limit Fetch to notes that belong to myEquipment
    //self
   // NSPredicate *predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"item.name like '%@'", self.item.name]];

    NSPredicate *predicate = [NSPredicate predicateWithFormat:PREDICATE GOES HERE];
    [fetchRequest setPredicate:predicate];



    [fetchRequest setFetchBatchSize:10];

    NSFetchedResultsController *frc = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest    
                                                                          managedObjectContext:self.myContext 
                                                                            sectionNameKeyPath:nil 
                                                                                     cacheName:@"Equipment"];
    //Set the Delegate Property
    frc.delegate = self;
    _fetchedResultsController = frc; 

    //Release Local Memory
    [sortDescriptor1 release];
    [sortDescriptor2 release];
    [sortDescriptors release];
    [fetchRequest release];

    return _fetchedResultsController;
 

【问题讨论】:

【参考方案1】:

看看core data documentation 和the predicate programming guide 你应该看到你必须做类似的事情

Equipment *equipment = // your equipment was passed from the previous tableview controller
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"equipment == %@", equipment];

【讨论】:

这不会导致逻辑错误的获取请求吗?查询将是 (设备 == (entity: Equipment...)

以上是关于如何为 CoreData 多对多关系编写 NSPredicate的主要内容,如果未能解决你的问题,请参考以下文章

如何为多对多关系创建 initial_data Json 夹具?

如何为连接表中的其他属性创建多对多 Hibernate 映射?

CoreData 多对多关系

CoreData 多对多关系插入和删除操作

保存循环删除多对多CoreData关系的一端

CoreData:如何建模循环多对多关系