【问题标题】:How to write an NSPredicate For CoreData to-many relationship如何为 CoreData 多对多关系编写 NSPredicate
【发布时间】: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;
} 

【问题讨论】:

    标签: core-data nsfetchedresultscontroller nspredicate


    【解决方案1】:

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

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

    【讨论】:

    • 这不会导致逻辑错误的获取请求吗?查询将是 (设备 == (entity: Equipment...)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多