代码之家  ›  专栏  ›  技术社区  ›  zs2020

nspredicate通过关系而不是属性筛选数据

  •  7
  • zs2020  · 技术社区  · 15 年前

    这是我的问题。我有一个模型,一个[事件]有多个[日],在[事件]中有一个叫做日的关系,在[日]中有一个相反的关系事件。

    现在,我要传递事件对象,并使用nsfetchedresultsController获取所有天数,并在表中填充所有天数。这是我的代码:

    // Create the fetch request for the entity.
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    // Edit the entity name as appropriate.
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Day" inManagedObjectContext:managedObjectContext];
    
    //set predicate  ->  ??
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"event = %@", self.currentEvent];
    [fetchRequest setPredicate:predicate];
    
    //set entity
    [fetchRequest setEntity:entity];
    
    // Set the batch size to a suitable number.
    [fetchRequest setFetchBatchSize:20];
    
    // Edit the sort key as appropriate.
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"date" ascending:YES];
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
    
    [fetchRequest setSortDescriptors:sortDescriptors];
    
    // Edit the section name key path and cache name if appropriate.
    // nil for section name key path means "no sections".
    NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest 
                                                                                                managedObjectContext:managedObjectContext 
                                                                                                  sectionNameKeyPath:nil
                                                                                                           cacheName:nil];
    

    不管怎样,nsfetchedResultsController实例将始终为不同的事件返回相同的内容。 我应该如何修复nspredicate?

    1 回复  |  直到 15 年前
        1
  •  14
  •   zs2020    15 年前

    我想出来了:

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF IN %@", self.currentEvent.days];
    
    推荐文章