I would like to create a custom fetch descriptor using the techniques described in this article. Specifically, I would like to limit properties retrieved using .propertiesToFetch
.
The project I am working on uses techniques described in this article to create a dynamic filter predicate. That is working well. What I can't figure out is how to marry the two.
The following is the init in my view that leverages the filtered data. How do I incorporate the concept of the custom fetch descriptor so that I only fetch certain properties?
It's not clear if/how I can do that with the _items = Query(filter: predicate, sort: sortDescriptors)
construct.
While I'm here ;-), in my everquest to learn, if there other ways to improve they way I'm handling the predicate please share!
I would appreciate any guidance. thanks -- jay
init(filterText: String, filter: Filter, settingsVM: SettingsViewModel) {
self.filter = filter
self.settingsVM = settingsVM
_journalViewType = State(initialValue: settingsVM.defaultJournalView)
// use computed variables for Predicate to work
let distantPast = Date.distantPast
let last7DaysFilter = filter.id == .last7
let last30DaysFilter = filter.id == .last30
let lastYearFilter = filter.id == .lastYear
let last7Days = Calendar.current.date(byAdding: .day, value: -7, to: Date.now)!
let last30Days = Calendar.current.date(byAdding: .month, value: -1, to: Date.now)!
let lastYear = Calendar.current.date(byAdding: .year, value: -1, to: Date.now)!
let sortDescriptors: [SortDescriptor<Item>] = [SortDescriptor(\Item.date, order: .reverse)]
// compound expression to check for date filter AND search text if it exists
let predicate = #Predicate<Item> { item in
if last7DaysFilter {
return item.date > last7Days && (item.text.localizedStandardContains(filterText) || (item.locationForFilterPredicate.localizedStandardContains(filterText)) || filterText.isEmpty)
} else if last30DaysFilter {
return item.date > last30Days && (item.text.localizedStandardContains(filterText) || (item.locationForFilterPredicate.localizedStandardContains(filterText)) || filterText.isEmpty)
} else if lastYearFilter {
return item.date > lastYear && (item.text.localizedStandardContains(filterText) || (item.locationForFilterPredicate.localizedStandardContains(filterText)) || filterText.isEmpty)
} else {
return item.date > distantPast && (item.text.localizedStandardContains(filterText) || (item.locationForFilterPredicate.localizedStandardContains(filterText)) || filterText.isEmpty)
}
}
_items = Query(filter: predicate, sort: sortDescriptors)
}