Core Data predicate : unimplemented SQL generation for predicate

前端 未结 5 1468
旧巷少年郎
旧巷少年郎 2020-12-09 10:50

Basically I got 3 entities in my data model : Brand, Model and Trim.

  • A brand has a one-to-many relationship with Model called \"models\". (one brand have multi
5条回答
  •  南笙
    南笙 (楼主)
    2020-12-09 11:25

    The keyword IN can be used but you cannot apply ANY at the same time as that does not make sense when you turn it into SQL.

    The predicate you are most likely looking for is:

    [NSPredicate predicateWithFormat:@"models.trims IN %@", arrayOfTrims];
    

    But that isn't going to work in this case either because you are going across a relationship. So what you need to do is reverse the whole thing:

    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Model"];
    [request setPredicate:[NSPredicate predicateWithFormat:@"trims in %@", arrayOfTrims]];
    
    NSError *error = nil;
    NSArray *modelArray = [moc executeFetchRequest:request error:&error];
    if (!modelArray) {
      NSLog(@"Error: %@\n%@", [error localizedDescription], [error userInfo]);
    }
    NSArray parentObjectArray = [modelArray valueForKey:@"${PARENT_RELATIONSHIP_NAME}"];
    

    Basically you are fetching the child objects to satisfy your ANY and then using KVC to retrieve the parent objects that you care about.

提交回复
热议问题