CosmicMind/Graph:搜索特定相关实体
CosmicMind/Graph: Search for Specific Related Entities
大家好,
从 CosmicMind (https://github.com/CosmicMind/Graph).
寻找对名为 Graph
的 CoreData
包装框架的支持
不幸的是,除了绝对基础知识之外几乎没有文档(尽管似乎引入了很多非常强大的概念 b.t.w)。
我想知道是否有人可以 - 请 - 帮我解决如何 select/filter 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" 且值为 [=] 的实体15=].
知道如何实现吗?
谢谢+祝福
这应该有效:
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
})
}
我们所做的是:
- 获取所有 "Unit" 个实体
- 过滤具有
isExpensive = false
的实体
- 并获取每个实体的 "UnitOfSection" 关系并测试该关系的对象是否为
sectionA
大家好,
从 CosmicMind (https://github.com/CosmicMind/Graph).
寻找对名为Graph
的 CoreData
包装框架的支持
不幸的是,除了绝对基础知识之外几乎没有文档(尽管似乎引入了很多非常强大的概念 b.t.w)。
我想知道是否有人可以 - 请 - 帮我解决如何 select/filter 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" 且值为 [=] 的实体15=].
知道如何实现吗?
谢谢+祝福
这应该有效:
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
})
}
我们所做的是:
- 获取所有 "Unit" 个实体
- 过滤具有
isExpensive = false
的实体
- 并获取每个实体的 "UnitOfSection" 关系并测试该关系的对象是否为
sectionA