核心数据谓词,用于检查子实体是否包含属性或"密钥路径"?

The*_* Pi 6 inheritance core-data objective-c nspredicate ios

我正在目标c中构建Core Data NSFetchRequest.在数据模型中,有一个抽象的父实体(包含4个基本属性),以及许多不同的子实体,它们包含不在父项中的属性.某些子项包含共享相同名称和数据类型的属性.

我将fetchRequest应用于父实体,以便它将搜索子实体的所有实例以查看是否存在"keypath".

我可以构造一个仅返回包含特定属性的子实体的谓词吗?

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%@ IN self.entityAttributes",attribute.name];
Run Code Online (Sandbox Code Playgroud)

这里,attribute是NSAttributeDescription的一个实例,目标是在其属性列表中搜索具有匹配名称属性的其他实体,但我不确定如何格式化谓词.

如果需要,我可以进一步澄清.谢谢!

eds*_*sko 5

您是否有理由检查实例而不是模型?您可以轻松找出哪些实体类型具有给定属性

for (NSEntityDescription* entityDescription in [self managedObjectModel]) {
  if ([[entityDescription propertiesByName] objectForKey:@"someProperty"] != nil) {
    // objects of this entity support the property you're looking for
  }
}
Run Code Online (Sandbox Code Playgroud)