【问题标题】:coredata fetch with predicate for object relation using swift使用swift使用谓词获取对象关系的coredata
【发布时间】:2017-10-28 02:43:48
【问题描述】:

我正在尝试获取 coreData 中的实体。该实体与另一个实体具有一对一的关系。我只想获取第一个实体中与第二个实体中的特定项目有关系的项目。我正在尝试这个我知道不正确的谓词:

let fetchRequest: NSFetchRequest<ItemA> = NSFetchRequest(entityName: "ItemA")
fetchRequest.predicate = NSPredicate(format: "itemA.itemB.itemBId == %d", Int(itemB.itemBId))

【问题讨论】:

  • 所以你想获得Id等于itemBitemA类型的项目,对吧?
  • 我可以通过下标itemA,itemB得到itemA的关系itemB。我只想在所有 itemA 中搜索 itemB 的 id 为 x 的那些。所以 itemA.itemB.itemBID = x.
  • 我不想要 Id 等于 itemB 的 itemA 类型的项目。我想要 itemA 类型的项目,其关系 itemB 的 id 为 itemBId,

标签: ios swift core-data


【解决方案1】:

当前您正在使用 itemA.itemB.itemBId ,这里您使用的是 itemA 的新实体,因为您不需要提供其名称,因为此谓词将应用于 itemA 实体,因此您只能使用 @ 987654323@在谓词内或者你可以使用SELF.itemB.itemBId(我不确定SELF或self,显然你可以查一下)。

所以我认为你可以获得itemA 类型的项目,其关系itemB 的id 为itemBId,如下所示:

let fetchRequest: NSFetchRequest<ItemA> = NSFetchRequest(entityName: "ItemA")
fetchRequest.predicate = NSPredicate(format: "itemB.itemBId == %d", Int(itemB.itemBId))

【讨论】:

  • 非常感谢。我有那个多余的 itemA 搞砸了。
猜你喜欢
  • 2011-01-19
  • 2013-10-24
  • 1970-01-01
  • 2019-03-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-22
  • 1970-01-01
相关资源
最近更新 更多