I am learning how to implement the new @SectionedFetchRequest with my Core Data object graph.
Here is my call...
@SectionedFetchRequest(
sectionIdentifier: \.sectionDay,
sortDescriptors: [
SortDescriptor(\.dateCommences, order: .reverse),
SortDescriptor(\.timeStampCreated, order: .reverse)],
animation: .default)
private var events: SectionedFetchResults<String, PTG_Event>
where PTG_Event
is the Core Data entity of results that I am expecting to be returned, sectionDay
is a convenience method that is prepared in an extension to PTG_Event
.
I have a user setting to display archived records (or not) using the entity attribute isArchived
and I am attempting to take advantage of the @AppStorage
wrapper as follows...
@AppStorage("preference_displayArchived") var kDisplayArchived = true
I cannot include a predicate in my sectioned fetch request, e.g.
predicate: NSPredicate(format: "isArchived == %@", NSNumber(booleanLiteral: kDisplayArchived)),
... because I receive the error ...
Cannot use instance member 'kDisplayArchived' within property initializer; property initializers run before 'self' is available
I understand this, but how do I create a dynamic reference to the app storage wrapper?
I am determined to figure out an easy method to use the user setting value preference_displayArchived
in the sectioned fetch request, so I have also attempted to hack a solution together using the method shown in #wwdc21-10017 for preparing a query for the .searchable
modifier, but have been so far unsuccessful in figuring out how to squeeze that into my List
code.
@State private var displayArchived = false
var queryArchived: Binding<Bool> {
Binding {
displayArchived
} set: { newValue in
displayArchived = newValue
events.nsPredicate = newValue == false ? nil : NSPredicate(format: "isArchived == %@", NSNumber(booleanLiteral: kDisplayArchived))
}
}
Any suggestions?