NSPredicate, get results with a subset of one-to-many relationship

后端 未结 3 1520
旧时难觅i
旧时难觅i 2021-01-07 02:45

I\'mm working around with Core Data and NSFetchedResultsController.

My Data Model looks like this:

Product with one-to-many relat

3条回答
  •  暖寄归人
    2021-01-07 02:52

    What you want to achieve could be reached in two ways:

    using a SUBQUERY

    [NSPredicate predicateWithFormat:@"SUBQUERY(dataLines, $x, $x.theWeek == %@).@count > 0)", [NSNumber numberWithInt:18]];
    

    or the ANY modifier

    [NSPredicate predicateWithFormat:@"ANY dataLines.theWeek == %@", [NSNumber numberWithInt:18]];
    

    You can do also the following if you need to check against multiple values:

    [NSPredicate predicateWithFormat:@"SUBQUERY(dataLines, $x, $x.theWeek == %@ or $x.theWeek == %@).@count > 0)", [NSNumber numberWithInt:18], [NSNumber numberWithInt:19]];
    

    The same can be applied to ANY modifier. ANY ... OR ANY ....

    Maybe if you share some code we could help you.

    P.S. I suppose you don't use scalar values and theWeek is a number.

    Hope it helps.

提交回复
热议问题