Swift Group Realm查询结果 [英] Swift group Realm query results
问题描述
我在Swift应用程序中关注了Realm对象(为简洁起见删除了init和其他非必要属性)
I've following Realm objects (init and other non-essential properties removed for brevity) in my Swift app
@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"
}
}
所以如果有2个项目成员
So if there are 2 project members
人员(id:1,名称:"Foo")
Person(id: 1, name: "Foo")
人员(id:2,名称:"Bar")
Person(id: 2, name: "Bar")
和多个项目,
Project(projectId:100,manager:,消息:[a1,a2,a3], lastMessage:)
Project(projectId: 100, manager: , messages: [a1, a2, a3], lastMessage: )
a1 =任务(项目:<#100>,任务ID:"a1",说明:任务1关于 项目100",createDate:Date())
a1 = Task(project: <#100>, taskId: "a1", description: "Task 1 about project 100", createDate: Date() )
a2 =任务(项目:<#100>,任务ID:"a2",说明:任务2关于 项目100",createDate:Date())
a2 = Task(project: <#100>, taskId: "a2", description: "Task 2 about project 100", createDate: Date() )
a3 =任务(项目:<#100>,任务ID:"a3",说明:任务3关于 项目100",createDate:Date())
a3 = Task(project: <#100>, taskId: "a3", description: "Task 3 about project 100", createDate: Date() )
Project(projectId:101,manager:,消息:[a1,a2,a3], lastMessage:)
Project(projectId: 101, manager: , messages: [a1, a2, a3], lastMessage: )
b1 =任务(项目:<#101>,任务ID:"b1",说明:任务1关于 项目101",createDate:Date())
b1 = Task(project: <#101>, taskId: "b1", description: "Task 1 about project 101", createDate: Date() )
b2 =任务(项目:<#101>,任务ID:"b2",说明:任务2关于 项目101",createDate:Date())
b2 = Task(project: <#101>, taskId: "b2", description: "Task 2 about project 101", createDate: Date() )
b3 =任务(项目:<#101>,任务ID:"b3",说明:任务3关于 项目101",createDate:Date())
b3 = Task(project: <#101>, taskId: "b3", description: "Task 3 about project 101", createDate: Date() )
b4 =任务(项目:<#101>,任务ID:"b3",说明:任务3关于 项目101",createDate:Date())
b4 = Task(project: <#101>, taskId: "b3", description: "Task 3 about project 101", createDate: Date() )
Project(projectId:102,manager:,消息:[a1,a2,a3], lastMessage:)
Project(projectId: 102, manager: , messages: [a1, a2, a3], lastMessage: )
c1 =任务(项目:<#102>,任务ID:"c1",说明:任务1关于 项目102",createDate:Date())
c1 = Task(project: <#102>, taskId: "c1", description: "Task 1 about project 102", createDate: Date() )
Project(projectId:103,manager:,消息:[a1,a2,a3], lastMessage:)
Project(projectId: 103, manager: , messages: [a1, a2, a3], lastMessage: )
d1 =任务(项目:<#103>,taskId:"d1",描述:任务1关于 项目103",createDate:Date())
d1 = Task(project: <#103>, taskId: "d1", description: "Task 1 about project 103", createDate: Date() )
d2 =任务(项目:<#103>,任务ID:"d2",说明:任务2关于 项目103",createDate:Date())
d2 = Task(project: <#103>, taskId: "d2", description: "Task 2 about project 103", createDate: Date() )
d3 =任务(项目:<#103>,任务ID:"d3",说明:任务3关于 项目103",createDate:Date())
d3 = Task(project: <#103>, taskId: "d3", description: "Task 3 about project 103", createDate: Date() )
在我的ProjectsViewController中
In my ProjectsViewController
我可以得到我的Realm结果
I can get my Realm results as
func getProjects() -> Results<Project> {
let results: Results<Conversation> = database
.objects(Conversation.self)
.sorted(byKeyPath: "lastTask.createDate", ascending: false)
return results
}
[附注-如果有更好的方法根据任务列表的最后一项对结果进行排序,请告诉我.这将使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
-------------------
问题:如何在Realm查询中对结果进行分组,以便获得结果数组的字典,例如
Question: How do I group the results in Realm query so I get a dictionary of grouped Array of results, such as
Foo-> [Project 100,任务数组],[Project 102,任务数组]
Foo -> [Project 100, Array of Tasks], [Project 102, Array of Tasks]
Bar-> [Project 101,任务数组],[Project 103,任务数组]
Bar -> [Project 101, Array of Tasks], [Project 103, Array of Tasks]
,并通过NotificationToken跟踪所有插入/更新.另外,我想按表格视图中的各个部分对它们进行布局.
and they're tracked via NotificationToken for all inserts / updates. 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.
如果我理解正确,您想找到一个以Person
作为键的字典,值是一个以Project ID(或Project)作为键和任务数组的值的字典.即
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. i.e.
let result : [Person:[Project:[Task]]] = .....
如果这是不正确的,您可以在这里接受这些想法,然后将它们按摩成您的理想状态.
If this isn't correct, you can probably take the ideas here and massage them into your ideal.
当您想按Person键输入时,我将向Person
类添加一个反向查找属性-这可能是您缺少的主要内容.请查看手册的这一部分以获取指导:( https://realm.io/docs/swift/latest/#inverse-relationships ),然后将此属性添加到Person
:
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. Look at this section of the manual for guidance: (https://realm.io/docs/swift/latest/#inverse-relationships), and then add this property to Person
:
let projects = LinkingObjects(fromType: Project.self, property: "manager")
然后我将向Person
添加一个计算属性,以检索项目和任务.这创建了一个数组,因为在问题中要求这样做.
I would then add a computed property to Person
to retrieve the projects and tasks. 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
}
如果您希望对任务进行排序,请在Projects
中添加一个名为sortedTasks的计算属性,为您执行此操作.
If you want the tasks to be sorted, then add a computed property to Projects
called sortedTasks that does that for you.
完成后,您可以在某个位置添加一个函数(Person
可以是全局的也可以是静态的),以将Person
的所有对象的结果添加到单个字典中.例如. :
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. E.g. :
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
}
}
原谅任何编译错误.
这篇关于Swift Group Realm查询结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!