如何在 SwiftUI 中按日期对核心数据项进行分组?
How to group core data items by date in SwiftUI?
我的 iOS 应用程序中有:
TO DO ITEMS
To do item 3
24/03/2020
------------
To do item 2
24/03/2020
------------
To do item 1
23/03/2020
------------
我想要的是:
TO DO ITEMS
24/03
To do item 3
24/03/2020
------------
To do item 2
24/03/2020
------------
23/03
To do item 1
23/03/2020
------------
===============
我目前有:
我正在使用 Core Data 并且有 1 个实体:Todo。模块:当前产品模块。代码生成:Class 定义。
这个实体有2个属性:标题(String)、日期(Date).
ContentView.swift
显示列表。
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>
@State private var show_modal: Bool = false
var dateFormatter: DateFormatter {
let formatter = DateFormatter()
formatter.dateStyle = .short
return formatter
}
// func to group items per date. Seemed to work at first, but crashes the app if I try to add new items using .sheet
func update(_ result : FetchedResults<Todo>)-> [[Todo]]{
return Dictionary(grouping: result){ (element : Todo) in
dateFormatter.string(from: element.date!)
}.values.map{[=12=]}
}
var body: some View {
NavigationView {
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)
// With this loop there is no crash, but it doesn't group items
//ForEach(Array(todos.enumerated()), id: \.element) {(i, todo) in
// HStack {
// Text(todo.title ?? "")
// Text("\(todo.date ?? Date(), formatter: self.dateFormatter)")
// }
//}
}
}
.navigationBarTitle(Text("To do items"))
.navigationBarItems(
trailing:
Button(action: {
self.show_modal = true
}) {
Text("Add")
}.sheet(isPresented: self.$show_modal) {
TodoAddView().environment(\.managedObjectContext, self.moc)
}
)
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
return ContentView().environment(\.managedObjectContext, context)
}
}
TodoAddView.swift
在此视图中,我添加了新项目。
import SwiftUI
struct TodoAddView: View {
@Environment(\.presentationMode) var presentationMode
@Environment(\.managedObjectContext) var moc
static let dateFormat: DateFormatter = {
let formatter = DateFormatter()
formatter.dateStyle = .medium
return formatter
}()
@State private var showDatePicker = false
@State private var title = ""
@State private var date : Date = Date()
var body: some View {
NavigationView {
VStack {
HStack {
Button(action: {
self.showDatePicker.toggle()
}) {
Text("\(date, formatter: Self.dateFormat)")
}
Spacer()
}
if self.showDatePicker {
DatePicker(
selection: $date,
displayedComponents: .date,
label: { Text("Date") }
)
.labelsHidden()
}
TextField("title", text: $title)
Spacer()
}
.padding()
.navigationBarTitle(Text("Add to do item"))
.navigationBarItems(
leading:
Button(action: {
self.presentationMode.wrappedValue.dismiss()
}) {
Text("Cancel")
},
trailing:
Button(action: {
let todo = Todo(context: self.moc)
todo.date = self.date
todo.title = self.title
do {
try self.moc.save()
}catch{
print(error)
}
self.presentationMode.wrappedValue.dismiss()
}) {
Text("Done")
}
)
}
}
}
struct TodoAddView_Previews: PreviewProvider {
static var previews: some View {
TodoAddView()
}
}
我试过这个:
我搜索了一些例子。一个看起来不错: 我从那里使用了更新函数和 ForEach,但它不适用于 SwiftUI 中的 .sheet。当我打开 .sheet(点击添加后)时,应用程序崩溃并出现错误:
Thread 1: Exception: "Attempt to create two animations for cell"
如何解决?还是有另一种按日期对核心数据进行分组的方法?有人告诉我应该将分组添加到我的数据模型中。稍后在 UI 中显示它。我不知道从哪里开始。
另一个猜测 是我也许可以编辑我的@FetchRequest 代码以在其中添加分组。但是我正在寻找几天没有运气的解决方案。
我知道核心数据中有一个 setPropertiesToGroupBy,但我不知道它是否以及如何与 @FetchRequest 和 SwiftUI.
一起工作
另一种猜测: 是否可以使用 Dictionary(grouping: attributeName) 在 SwiftUI 中根据属性对 CoreData 实体实例进行分组?
分组数组看起来很简单: https://www.hackingwithswift.com/example-code/language/how-to-group-arrays-using-dictionaries ,但我不知道它是否以及如何与 Core Data 和 @FetchRequest 一起工作。
我自己刚接触 SwiftUI,所以这可能是一种误解,但我认为问题在于 update
函数不稳定,因为它不能保证 [=14] =] 每次都以相同的顺序分组。因此,当添加新项目时,SwiftUI 会感到困惑。我发现通过专门对数组进行排序可以避免错误:
func update(_ result : FetchedResults<Todo>)-> [[Todo]]{
return Dictionary(grouping: result){ (element : Todo) in
dateFormatter.string(from: element.date!)
}.values.sorted() { [=10=][0].date! < [0].date! }
}
我对编程也很陌生,以下解决方案可能不够优雅,但 ngl 我很自豪自己能弄明白!
我向名为 lastOfDay 的对象添加了一个 bool,它触发了该对象上日期的文本视图:
ForEach(allResults) { result in
VStack(spacing: 0) {
if currentMethod == .byDate {
if result.lastOfDay {
Text("\(result.date, formatter: dateFormatter)")
}
}
ListView(result: result)
}
}
然后我有一个 onAppear 函数,它将我获取的结果复制到一个单独的非 CoreData 数组,按日期组织它们并检查下一个结果的日期是否与当前对象的日期不同 - 并翻转必要的布尔值。我希望通过某些版本的 .map 实现这一点,但认为有必要考虑列表为空或只有一个项目的情况。
if allResults.count != 0 {
if allResults.count == 1 {
allResults[0].lastOfDay = true
}
for i in 0..<(allResults.count-1) {
if allResults.count > 1 {
allResults[0].lastOfDay = true
if allResults[i].date.hasSame(.day, as: allResults[i+1].date) {
allResults[i+1].lastOfDay = false
} else {
allResults[i+1].lastOfDay = true
}
}
}
}
我在 上选择的 hasSame 日期扩展方法 如果您希望让用户删除批次,我不知道这种方法的效果如何,但它对我来说非常适合,因为我只想实施单一删除或删除所有对象(但是,由于每次发生此类更改时我都会触发过滤过程 - 使用更大的数据集可能会变得昂贵)。
将分组嵌入托管对象模型是可行的方法,因为它会更健壮并且可以很好地处理大型数据集。我已经提供了 with a sample project 如何实施它。
当我们在 Dictionary
上使用 init(grouping:by:)
时,我们可能会在每次执行任何列表操作(例如插入或删除)时重新编译整个列表,并由进行分组的字典支持,这不是高性能的,在我的例子中,会导致锯齿状的动画。从存储中获取以一种方式排序的实体然后再次在本地进行排序以将它们分成几个部分在性能方面没有意义。
据我所知,使用提取请求分组是不可能的,因为 propertiesToGroupBy
是使用 SQL GROUP BY
查询的核心数据接口,旨在与聚合函数(例如最小值、最大值、总和)而不是将数据集分成多个部分。
我的 iOS 应用程序中有:
TO DO ITEMS
To do item 3
24/03/2020
------------
To do item 2
24/03/2020
------------
To do item 1
23/03/2020
------------
我想要的是:
TO DO ITEMS
24/03
To do item 3
24/03/2020
------------
To do item 2
24/03/2020
------------
23/03
To do item 1
23/03/2020
------------
===============
我目前有:
我正在使用 Core Data 并且有 1 个实体:Todo。模块:当前产品模块。代码生成:Class 定义。 这个实体有2个属性:标题(String)、日期(Date).
ContentView.swift 显示列表。
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>
@State private var show_modal: Bool = false
var dateFormatter: DateFormatter {
let formatter = DateFormatter()
formatter.dateStyle = .short
return formatter
}
// func to group items per date. Seemed to work at first, but crashes the app if I try to add new items using .sheet
func update(_ result : FetchedResults<Todo>)-> [[Todo]]{
return Dictionary(grouping: result){ (element : Todo) in
dateFormatter.string(from: element.date!)
}.values.map{[=12=]}
}
var body: some View {
NavigationView {
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)
// With this loop there is no crash, but it doesn't group items
//ForEach(Array(todos.enumerated()), id: \.element) {(i, todo) in
// HStack {
// Text(todo.title ?? "")
// Text("\(todo.date ?? Date(), formatter: self.dateFormatter)")
// }
//}
}
}
.navigationBarTitle(Text("To do items"))
.navigationBarItems(
trailing:
Button(action: {
self.show_modal = true
}) {
Text("Add")
}.sheet(isPresented: self.$show_modal) {
TodoAddView().environment(\.managedObjectContext, self.moc)
}
)
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
return ContentView().environment(\.managedObjectContext, context)
}
}
TodoAddView.swift 在此视图中,我添加了新项目。
import SwiftUI
struct TodoAddView: View {
@Environment(\.presentationMode) var presentationMode
@Environment(\.managedObjectContext) var moc
static let dateFormat: DateFormatter = {
let formatter = DateFormatter()
formatter.dateStyle = .medium
return formatter
}()
@State private var showDatePicker = false
@State private var title = ""
@State private var date : Date = Date()
var body: some View {
NavigationView {
VStack {
HStack {
Button(action: {
self.showDatePicker.toggle()
}) {
Text("\(date, formatter: Self.dateFormat)")
}
Spacer()
}
if self.showDatePicker {
DatePicker(
selection: $date,
displayedComponents: .date,
label: { Text("Date") }
)
.labelsHidden()
}
TextField("title", text: $title)
Spacer()
}
.padding()
.navigationBarTitle(Text("Add to do item"))
.navigationBarItems(
leading:
Button(action: {
self.presentationMode.wrappedValue.dismiss()
}) {
Text("Cancel")
},
trailing:
Button(action: {
let todo = Todo(context: self.moc)
todo.date = self.date
todo.title = self.title
do {
try self.moc.save()
}catch{
print(error)
}
self.presentationMode.wrappedValue.dismiss()
}) {
Text("Done")
}
)
}
}
}
struct TodoAddView_Previews: PreviewProvider {
static var previews: some View {
TodoAddView()
}
}
我试过这个:
我搜索了一些例子。一个看起来不错:
Thread 1: Exception: "Attempt to create two animations for cell"
如何解决?还是有另一种按日期对核心数据进行分组的方法?有人告诉我应该将分组添加到我的数据模型中。稍后在 UI 中显示它。我不知道从哪里开始。
另一个猜测 是我也许可以编辑我的@FetchRequest 代码以在其中添加分组。但是我正在寻找几天没有运气的解决方案。 我知道核心数据中有一个 setPropertiesToGroupBy,但我不知道它是否以及如何与 @FetchRequest 和 SwiftUI.
一起工作另一种猜测: 是否可以使用 Dictionary(grouping: attributeName) 在 SwiftUI 中根据属性对 CoreData 实体实例进行分组? 分组数组看起来很简单: https://www.hackingwithswift.com/example-code/language/how-to-group-arrays-using-dictionaries ,但我不知道它是否以及如何与 Core Data 和 @FetchRequest 一起工作。
我自己刚接触 SwiftUI,所以这可能是一种误解,但我认为问题在于 update
函数不稳定,因为它不能保证 [=14] =] 每次都以相同的顺序分组。因此,当添加新项目时,SwiftUI 会感到困惑。我发现通过专门对数组进行排序可以避免错误:
func update(_ result : FetchedResults<Todo>)-> [[Todo]]{
return Dictionary(grouping: result){ (element : Todo) in
dateFormatter.string(from: element.date!)
}.values.sorted() { [=10=][0].date! < [0].date! }
}
我对编程也很陌生,以下解决方案可能不够优雅,但 ngl 我很自豪自己能弄明白!
我向名为 lastOfDay 的对象添加了一个 bool,它触发了该对象上日期的文本视图:
ForEach(allResults) { result in
VStack(spacing: 0) {
if currentMethod == .byDate {
if result.lastOfDay {
Text("\(result.date, formatter: dateFormatter)")
}
}
ListView(result: result)
}
}
然后我有一个 onAppear 函数,它将我获取的结果复制到一个单独的非 CoreData 数组,按日期组织它们并检查下一个结果的日期是否与当前对象的日期不同 - 并翻转必要的布尔值。我希望通过某些版本的 .map 实现这一点,但认为有必要考虑列表为空或只有一个项目的情况。
if allResults.count != 0 {
if allResults.count == 1 {
allResults[0].lastOfDay = true
}
for i in 0..<(allResults.count-1) {
if allResults.count > 1 {
allResults[0].lastOfDay = true
if allResults[i].date.hasSame(.day, as: allResults[i+1].date) {
allResults[i+1].lastOfDay = false
} else {
allResults[i+1].lastOfDay = true
}
}
}
}
我在
将分组嵌入托管对象模型是可行的方法,因为它会更健壮并且可以很好地处理大型数据集。我已经提供了
当我们在 Dictionary
上使用 init(grouping:by:)
时,我们可能会在每次执行任何列表操作(例如插入或删除)时重新编译整个列表,并由进行分组的字典支持,这不是高性能的,在我的例子中,会导致锯齿状的动画。从存储中获取以一种方式排序的实体然后再次在本地进行排序以将它们分成几个部分在性能方面没有意义。
据我所知,使用提取请求分组是不可能的,因为 propertiesToGroupBy
是使用 SQL GROUP BY
查询的核心数据接口,旨在与聚合函数(例如最小值、最大值、总和)而不是将数据集分成多个部分。