What's better way to build NSPredicate with to-many deep relationships?

后端 未结 2 2059
名媛妹妹
名媛妹妹 2020-12-14 02:36

I have three entities: EntityA, EntityB and EntityC connected with to-many relationships.

See schema for details:

alt text http://img706.imageshack.us/img706

2条回答
  •  鱼传尺愫
    2020-12-14 03:16

    While I was stopped at the following decision:

    First, I get all the EntityC that satisfy the condition EntityC.name equal to 'SomeName'

    NSPredicate *p = [NSPredicate predicateWithFormat:@"name like %@", @"SomeName];
    

    ...

    NSArray *res = [managedObjectContext executeFetchRequest:fetchRequest error:&error];
    

    Then I get an array of EntityB from above query

    NSArray *parentBs = [res valueForKeyPath:@"@distinctUnionOfObjects.parent"];
    

    Than get array of EntityB that satisfy the condition EntityB.EntitiesC.name equal to 'SomeName':

    NSExpression *leftExpression = [NSExpression expressionForEvaluatedObject];
    NSExpression *rightExpression = [NSExpression expressionForConstantValue:parentBs];
    
    NSPredicate *p = [NSComparisonPredicate predicateWithLeftExpression:leftExpression rightExpression: rightExpression modifier:NSDirectPredicateModifier type:NSInPredicateOperatorType options:0];
    

    I repeat the same for EntityA.

    The effectiveness of this solution in doubt and I still expect a better solution for this problem.

提交回复
热议问题