How to create an NSFetchRequest which filters Core Data objects based on attributes AND relationships?

后端 未结 1 1593
灰色年华
灰色年华 2020-12-23 02:20

I have a Core Data model setup like so:

  • Blockbuster Entity
    • To-Many relationship to DVD entities.
1条回答
  •  生来不讨喜
    2020-12-23 02:52

    You can traverse relationships in an NSPredicate. For example, you could write something like

    [NSPredicate predicateWithFormat:@"title == %@ AND blockbuster.name LIKE \"Blockbuster C\"", @"Transformers 2"]
    

    Now, if you don't have a property to compare against and you need to check actual objects, then you could use something like

    [NSPredicate predicateWithFormat:@"title == %@ AND blockbuster IN %@", @"Transformers 2", setOfBlockbusters]
    

    The full syntax is documented here. But setOfBlockbusters could be a set, an array, or a dictionary (if it's a dictionary, the values, not the keys, are used).

    0 讨论(0)
提交回复
热议问题