如何按日期正确地对从 CoreData 获取的列表进行分组? [英] How to properly group a list fetched from CoreData by date?
问题描述
为了简单起见,假设我想创建一个简单的待办事项应用程序.我的 xcdatamodeld 中有一个实体 Todo,其属性为 id
、title
和 date
,以及以下 swiftui 视图(如图所示):>
For the sake of simplicity lets assume I want to create a simple todo app. I have an entity Todo in my xcdatamodeld with the properties id
, title
and date
, and the following swiftui view (example pictured):
import SwiftUI
struct ContentView: View {
@Environment(.managedObjectContext) var moc
@State private var date = Date()
@FetchRequest(
entity: Todo.entity(),
sortDescriptors: [
NSSortDescriptor(keyPath: Todo.date, ascending: true)
]
) var todos: FetchedResults<Todo>
var dateFormatter: DateFormatter {
let formatter = DateFormatter()
formatter.dateStyle = .short
return formatter
}
var body: some View {
VStack {
List {
ForEach(todos, id: .self) { todo in
HStack {
Text(todo.title ?? "")
Text("(todo.date ?? Date(), formatter: self.dateFormatter)")
}
}
}
Form {
DatePicker(selection: $date, in: ...Date(), displayedComponents: .date) {
Text("Datum")
}
}
Button(action: {
let newTodo = Todo(context: self.moc)
newTodo.title = String(Int.random(in: 0 ..< 100))
newTodo.date = self.date
newTodo.id = UUID()
try? self.moc.save()
}, label: {
Text("Add new todo")
})
}
}
}
待办事项在获取时按日期排序,并显示在如下列表中:
The todos are sorted by date upon fetching, and are displayed in a list like this:
我想根据每个待办事项各自的日期对列表进行分组(模型):
I want to group the list based on each todos respective date as such (mockup):
根据我的理解,这可以与 init()
函数中的 Dictionaries 一起使用,但是我想不出任何远程有用的东西.有没有一种有效的方法来分组数据?
From my understanding this could work with Dictionaries in the init()
function, however I couldn't come up with anything remotely useful. Is there an efficient way to group data?
推荐答案
您可以尝试以下方法,它应该适用于您的情况.
You may try the following, It should work in your situation.
@Environment(.managedObjectContext) var moc
@State private var date = Date()
@FetchRequest(
entity: Todo.entity(),
sortDescriptors: [
NSSortDescriptor(keyPath: Todo.date, ascending: true)
]
) var todos: FetchedResults<Todo>
var dateFormatter: DateFormatter {
let formatter = DateFormatter()
formatter.dateStyle = .short
return formatter
}
func update(_ result : FetchedResults<Todo>)-> [[Todo]]{
return Dictionary(grouping: result){ (element : Todo) in
dateFormatter.string(from: element.date!)
}.values.map{$0}
}
var body: some View {
VStack {
List {
ForEach(update(todos), id: .self) { (section: [Todo]) in
Section(header: Text( self.dateFormatter.string(from: section[0].date!))) {
ForEach(section, id: .self) { todo in
HStack {
Text(todo.title ?? "")
Text("(todo.date ?? Date(), formatter: self.dateFormatter)")
}
}
}
}.id(todos.count)
}
Form {
DatePicker(selection: $date, in: ...Date(), displayedComponents: .date) {
Text("Datum")
}
}
Button(action: {
let newTodo = Todo(context: self.moc)
newTodo.title = String(Int.random(in: 0 ..< 100))
newTodo.date = self.date
newTodo.id = UUID()
try? self.moc.save()
}, label: {
Text("Add new todo")
})
}
}
这篇关于如何按日期正确地对从 CoreData 获取的列表进行分组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!