【发布时间】:2018-08-27 20:14:23
【问题描述】:
大家好,
从 CosmicMind (https://github.com/CosmicMind/Graph) 寻求对名为 Graph 的 CoreData 包装框架的支持。
不幸的是,除了绝对基础知识之外,几乎没有什么文档(尽管似乎有很多非常强大的概念引入了 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”关系并且具有值为false 的属性“isExpensive”的实体。
知道如何实现吗?
谢谢+祝福
【问题讨论】:
-
试试:
let units = unitsSearch.sync().filter { ($0["isExpensive"] as? Bool) == false && !$0.relationship(types: "UnitOfSection").isEmpty } -
嗨@OrkhanAlikhanov,非常感谢您的评论。我更新了示例以使其更加精确并添加了第四个实体。预期结果应该只有一个实体(即 unitA,因为它与 sectionA 具有“UnitOfSection”关系并且具有“isExpensive”== false 的属性)。您的方法似乎只过滤了 isExpensive == false 项目并导致计数为 2。请参见此处的操场:github.com/karstengresch/graphplaygrounds(子页面:“图表关系 I”,实际上我还不想将其发布为还没准备好。)
-
啊,真是太棒了!仅供参考,有一个新的 API 已合并。见CosmicMind/Graph#/154
-
您可以检查以下过滤器关闭吗?
($0["isExpensive"] as? Bool) == false && $0.relationship(types: "UnitOfSection").contains(sectionA) -
感谢@OrkhanAlikhanov!不幸的是 Graph dev 分支没有在这里编译(得到 Search.swift:72:27: error: value of type 'Predicate' has no member 'map' self.predicate = self.predicate.map { ~~~~~^~ ~~~~~~~ ~~~ 对于大多数 Graph API 类)。使用 2.2.2,我得到:“错误:无法将 'Entity' 类型的值转换为预期的参数类型 'Relationship' ($0["isExpensive"] as?Bool) == false && $0.relationship(types: "UnitOfSection" ).contains(sectionA) ^~~~~~~~"
标签: ios core-data graph frameworks cosmicmind