[英]Swift group Realm query results
I've following Realm objects (init and other non-essential properties removed for brevity) in my Swift app 我在Swift应用程序中跟踪了Realm对象(为了简洁而删除了init和其他非必要属性)
@objcMembers
class Person: Object {
dynamic var id: String = ""
dynamic var name: String = ""
override static func primaryKey() -> String? {
return "id"
}
}
@objcMembers
class Project: Object {
dynamic var projectId: Int = 0
dynamic var manager: Person?
var tasks = List<Task>()
dynamic var lastTask: Task?
override static func primaryKey() -> String? {
return "projectId"
}
}
@objcMembers
class Task : Object {
dynamic var project = LinkingObjects(fromType: Project.self, property: "tasks")
dynamic var taskId: String = ""
dynamic var description: String = ""
dynamic var createDate: Date = Date()
override static func primaryKey() -> String? {
return "taskId"
}
}
So if there are 2 project members 所以如果有2个项目成员
Person(id: 1, name: "Foo") 人(身份证号码:1,姓名:“Foo”)
Person(id: 2, name: "Bar") 人(身份证号码:2,姓名:“酒吧”)
and multiple projects, 和多个项目,
Project(projectId: 100, manager: , messages: [a1, a2, a3], lastMessage: ) Project(projectId:100,manager:,messages:[a1,a2,a3],lastMessage :)
a1 = Task(project: <#100>, taskId: "a1", description: "Task 1 about project 100", createDate: Date() ) a1 =任务(项目:<#100>,任务编号:“ a1”,描述:“关于项目100的任务1”,createDate:Date())
a2 = Task(project: <#100>, taskId: "a2", description: "Task 2 about project 100", createDate: Date() ) a2 = Task(项目:<#100>,taskId:“ a2”,描述:“关于项目100的任务2”,createDate:Date())
a3 = Task(project: <#100>, taskId: "a3", description: "Task 3 about project 100", createDate: Date() ) a3 = Task(项目:<#100>,taskId:“ a3”,描述:“关于项目100的任务3”,createDate:Date())
Project(projectId: 101, manager: , messages: [a1, a2, a3], lastMessage: ) 项目(projectId:101,经理:,消息:[a1,a2,a3],lastMessage :)
b1 = Task(project: <#101>, taskId: "b1", description: "Task 1 about project 101", createDate: Date() ) b1 = Task(项目:<#101>,taskId:“ b1”,描述:“关于项目101的任务1”,createDate:Date())
b2 = Task(project: <#101>, taskId: "b2", description: "Task 2 about project 101", createDate: Date() ) b2 = Task(项目:<#101>,taskId:“ b2”,描述:“关于项目101的任务2”,createDate:Date())
b3 = Task(project: <#101>, taskId: "b3", description: "Task 3 about project 101", createDate: Date() ) b3 = Task(项目:<#101>,taskId:“ b3”,描述:“关于项目101的任务3”,createDate:Date())
b4 = Task(project: <#101>, taskId: "b3", description: "Task 3 about project 101", createDate: Date() ) b4 = Task(项目:<#101>,taskId:“ b3”,描述:“关于项目101的任务3”,createDate:Date())
Project(projectId: 102, manager: , messages: [a1, a2, a3], lastMessage: ) 项目(projectId:102,经理:,消息:[a1,a2,a3],lastMessage :)
c1 = Task(project: <#102>, taskId: "c1", description: "Task 1 about project 102", createDate: Date() ) c1 = Task(项目:<#102>,taskId:“ c1”,描述:“关于项目102的任务1”,createDate:Date())
Project(projectId: 103, manager: , messages: [a1, a2, a3], lastMessage: ) Project(projectId:103,manager:,messages:[a1,a2,a3],lastMessage :)
d1 = Task(project: <#103>, taskId: "d1", description: "Task 1 about project 103", createDate: Date() ) d1 = Task(项目:<#103>,taskId:“ d1”,描述:“关于项目103的任务1”,createDate:Date())
d2 = Task(project: <#103>, taskId: "d2", description: "Task 2 about project 103", createDate: Date() ) d2 = Task(项目:<#103>,taskId:“ d2”,描述:“关于项目103的任务2”,createDate:Date())
d3 = Task(project: <#103>, taskId: "d3", description: "Task 3 about project 103", createDate: Date() ) d3 = Task(项目:<#103>,taskId:“ d3”,描述:“关于项目103的任务3”,createDate:Date())
In my ProjectsViewController 在我的ProjectsViewController中
I can get my Realm results as 我可以将我的Realm结果作为
func getProjects() -> Results<Project> {
let results: Results<Conversation> = database
.objects(Conversation.self)
.sorted(byKeyPath: "lastTask.createDate", ascending: false)
return results
}
[Side note - If there is a better way of sorting the results based on last item of tasks List then do let me know. [旁注 - 如果有更好的方法根据最后一项任务列表对结果进行排序,那么请告诉我。 That'll make the use lastTask var redundant.] 这将使得lastTask var变得多余。
which will display in my table view as 这将在我的表视图中显示为
===================
Projects
-------------------
Foo
Project 100
Task a3
-------------------
Bar
Project 101
Task b4
-------------------
Foo
Project 102
Task c1
-------------------
Bar
Project 103
Task d3
-------------------
Question: How do I group the results in Realm query so I get a dictionary of grouped Array of results, such as 问题:如何在Realm查询中对结果进行分组,以便获得分组结果数组的字典,例如
Foo -> [Project 100, Array of Tasks], [Project 102, Array of Tasks] Foo - > [Project 100,任务数组],[Project 102,Tasks of Tasks]
Bar -> [Project 101, Array of Tasks], [Project 103, Array of Tasks] 栏 - > [项目101,任务数组],[项目103,任务数组]
and they're tracked via NotificationToken for all inserts / updates. 并且通过NotificationToken跟踪它们以进行所有插入/更新。 Also I want to dislay them grouped by sections in the table view. 另外,我想在表格视图中按部分分组。
===================
Projects (Grouped)
-------------------
Foo (Section Header)
-------------------
Project 100
Task a3
-------------------
Project 102
Task c1
-------------------
-------------------
Bar (Section Header)
-------------------
Project 101
Task b4
-------------------
Project 103
Task d3
-------------------
I'll answer based on what I think you're asking. 我会根据我的想法回答。
If I understand correctly, you want to find a dictionary with the Person
as the key, the values being a dictionary with Project ID (or Project) as the key and values of an array of tasks. 如果我理解正确,您想找到一个以Person
为键的字典,值是一个以Project ID(或Project)为键的字典,以及一个任务数组的值。 ie 即
let result : [Person:[Project:[Task]]] = .....
If this isn't correct, you can probably take the ideas here and massage them into your ideal. 如果这不正确,你可以把这些想法带到这里并按照它们的理想进行按摩。
As you want to key by Person, I'd add a reverse lookup property to the Person
class - and this is probably the main thing you're missing. 当你想按Person键入时,我会向Person
类添加一个反向查找属性 - 这可能是你遗漏的主要内容。 Look at this section of the manual for guidance: ( https://realm.io/docs/swift/latest/#inverse-relationships ), and then add this property to Person
: 请查看手册的本部分以获取指导:( https://realm.io/docs/swift/latest/#inverse-relationships ),然后将此属性添加到Person
:
let projects = LinkingObjects(fromType: Project.self, property: "manager")
I would then add a computed property to Person
to retrieve the projects and tasks. 然后我会向Person
添加一个计算属性来检索项目和任务。 This creates an array since that was asked for in the question. 这创建了一个数组,因为在问题中要求这样做。
var projectsAndTasks : [Project:[Task]]
{
var result : [Project:[Task]] = [:]
for project in projects
{
result[project] = project.tasks.map { $0 }
}
return result
}
If you want the tasks to be sorted, then add a computed property to Projects
called sortedTasks that does that for you. 如果要对任务进行排序,则将计算属性添加到名为sortedTasks的Projects
,为您执行此操作。
Once that's done, you can add a function somewhere (either global or static to Person
) to add the results for all objects of Person
into a single dictionary. 一旦这样做了,你可以再添加一个函数(全局或静态到Person
),为的所有对象的结果加Person
到一个单一的字典。 Eg : 例如:
extension Realm
{
var allPersonsAndProjects : [Person:[Project:[Task]]]
{
var result : [Person:[Project:[Task]]] = []
let persons = objects(Person.self)
for person in persons
{
result[person] = person.projectsAndTasks
}
return result
}
}
Forgive any compile errors. 原谅任何编译错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.