CosmicMind / Graph:搜索特定的相关实体
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CosmicMind / Graph:搜索特定的相关实体相关的知识,希望对你有一定的参考价值。
大家好,
寻找对来自CosmicMind(CoreData
)的Graph
包装框架https://github.com/CosmicMind/Graph的支持。
不幸的是,除了绝对的基础知识之外,很少有文档(尽管b.t.w中似乎有很多非常强大的概念)。
我想知道是否有人可以 - 请帮助我如何选择/过滤1:N关系中的实体。
鉴于以下结构......
import Graph
let graph = Graph()
graph.clear()
let sectionA = Entity(type: "Section")
sectionA["id"] = 12
sectionA["name"] = "SectionA"
let sectionB = Entity(type: "Section")
sectionB["id"] = 2
sectionB["name"] = "SectionB"
let unitA = Entity(type: "Unit")
unitA["id"] = 122
unitA["name"] = "UnitA"
unitA["isExpensive"] = false
unitA.is(relationship: "UnitOfSection").of(sectionA)
let unitB = Entity(type: "Unit")
unitB["id"] = 19
unitB["name"] = "UnitB"
unitB["isExpensive"] = false
unitB.is(relationship: "UnitOfSection").of(sectionB)
let unitC = Entity(type: "Unit")
unitC["id"] = 7
unitC["name"] = "UnitC"
unitC["isExpensive"] = true
unitC.is(relationship: "UnitOfSection").of(sectionA)
let unitD = Entity(type: "Unit")
unitD["name"] = "UnitD"
unitD["isExpensive"] = true
unitD["id"] = 4
unitD.is(relationship: "UnitOfSection").of(sectionA)
graph.sync()
let unitsSearch = Search<Entity>(graph: graph).for(types: "Unit")
let units = unitsSearch.sync()
...我想只得到那些与sectionA
有“UnitOfSection”关系的实体,它们的属性为“isExpensive”,值为false
。
知道怎么做到这一点?
谢谢+祝福
答案
这应该工作:
let units = Search<Entity>(graph: graph).for(types: "Unit").sync().filter { entity in
(entity["isExpensive"] as? Bool) == false &&
entity.relationship(types: "UnitOfSection").contains(where: { relationship in
relationship.object == sectionA
})
}
我们做的是:
- 获取所有“单位”实体
- 过滤具有
isExpensive = false
的实体 - 并获取每个实体的“UnitOfSection”关系,并测试该关系的对象是否为
sectionA
以上是关于CosmicMind / Graph:搜索特定的相关实体的主要内容,如果未能解决你的问题,请参考以下文章
CosmicMind Graph + iCloud 和 Today Extension
CosmicMind/Material - 如何在 NavigationDrawerController 中调用 pushViewController?