我在 Core Data 中的两个实体(EntityA 和 EntityB)之间存在多对多关系,并且我试图通过删除与 EntityA 的任何实例不再有任何关系的 EntityB 实例来定期清理数据库。我正在使用Mogenerator创建 .m 和 .h 文件,这允许我以我的方式引用属性/关系名称。
NSPredicate *noRelationPredicate = [NSComparisonPredicate predicateWithLeftExpression:[NSExpression expressionWithFormat:@"%K", EntityBRelationships.relationshipNameForEntityA] rightExpression:[NSExpression expressionWithFormat:@"nil"] modifier:NSDirectPredicateModifier type:NSEqualToPredicateOperatorType options:0];
但是,我在执行 fetch 时遇到以下错误:'NSInvalidArgumentException',原因:'to-many key not allowed here'
我更喜欢使用类方法NSComparisonPredicate predicateWithLeftExpression:rightExpression:modifier:type:options:创建谓词,因为我尽量避免在谓词创建中使用字符串文字。