如何按日期过滤核心数据项?
How do i filter core data items by date?
我正在尝试显示在用户通过 DatePicker 选择的特定日期保存到 Core Data 的数据。
数据保存如下,日期为:
func saveBreakfast() {
let newBreakfastItem = BreakfastItem(context: self.moc)
newBreakfastItem.id = UUID()
newBreakfastItem.name = self.item.name
newBreakfastItem.calories = Int32(self.totalCalories)
newBreakfastItem.carbs = Int32(self.totalCarbs)
newBreakfastItem.protein = Int32(self.totalProtein)
newBreakfastItem.fat = Int32(self.totalFats)
newBreakfastItem.date = self.dateAdded
do {
if self.mocB.hasChanges { // saves only if changes are made
try? self.mocB.save()
}
}
}
我目前有
@State var selectedDate : Date
&
ForEach(self.BreakfastItems.filter { [=12=].date == selectedDate }, id: \.id) { newBreakfastItems in
但是没有任何显示,知道这是为什么吗?这两个日期格式不正确吗?
或者有其他方法可以实现吗?
提前致谢!
您可以使用以下函数从 Core Data 加载特定日期的早餐项目:
func loadBreakfastItemsFromCoreData(at date: Date) -> [BreakfastItems] {
let request: NSFetchRequest<BreakfastItems> = BreakfastItems.fetchRequest()
let startDate = Calendar.current.startOfDay(date)
var components = DateComponents()
components.day = 1
components.second = -1
let endDate = Calendar.current.date(byAdding: components, to: startDate)!
request.predicate = NSPredicate(format: "date >= %@ AND date <= %@", startDate, endDate)
// Optional: You can sort by date
request.sortDescriptors = [NSSortDescriptor(key: "date", ascending: true)]
do {
return try mocB.fetch(request)
} catch {
print("Error fetching data from context: \(error)")
}
return []
}
如果您使用不同的上下文,请确保您知道如何操作。如果您不知道,请阅读它,或者只使用一个上下文。在我的示例中,我使用了您的背景上下文,但您可以随意切换到主要上下文。
我正在尝试显示在用户通过 DatePicker 选择的特定日期保存到 Core Data 的数据。
数据保存如下,日期为:
func saveBreakfast() {
let newBreakfastItem = BreakfastItem(context: self.moc)
newBreakfastItem.id = UUID()
newBreakfastItem.name = self.item.name
newBreakfastItem.calories = Int32(self.totalCalories)
newBreakfastItem.carbs = Int32(self.totalCarbs)
newBreakfastItem.protein = Int32(self.totalProtein)
newBreakfastItem.fat = Int32(self.totalFats)
newBreakfastItem.date = self.dateAdded
do {
if self.mocB.hasChanges { // saves only if changes are made
try? self.mocB.save()
}
}
}
我目前有
@State var selectedDate : Date
&
ForEach(self.BreakfastItems.filter { [=12=].date == selectedDate }, id: \.id) { newBreakfastItems in
但是没有任何显示,知道这是为什么吗?这两个日期格式不正确吗?
或者有其他方法可以实现吗?
提前致谢!
您可以使用以下函数从 Core Data 加载特定日期的早餐项目:
func loadBreakfastItemsFromCoreData(at date: Date) -> [BreakfastItems] {
let request: NSFetchRequest<BreakfastItems> = BreakfastItems.fetchRequest()
let startDate = Calendar.current.startOfDay(date)
var components = DateComponents()
components.day = 1
components.second = -1
let endDate = Calendar.current.date(byAdding: components, to: startDate)!
request.predicate = NSPredicate(format: "date >= %@ AND date <= %@", startDate, endDate)
// Optional: You can sort by date
request.sortDescriptors = [NSSortDescriptor(key: "date", ascending: true)]
do {
return try mocB.fetch(request)
} catch {
print("Error fetching data from context: \(error)")
}
return []
}
如果您使用不同的上下文,请确保您知道如何操作。如果您不知道,请阅读它,或者只使用一个上下文。在我的示例中,我使用了您的背景上下文,但您可以随意切换到主要上下文。