I am building a File Manager app.
In this App the user can optionally select a Folder. And I am building now a SwiftUI view to show the files of the selected folder, or ALL files if there are no selection.
I do have the selected folder in a AppState class:
class AppState: ObservableObject {
@Published var selectedFolder: Folder?
}
And I am trying to build my FileListView using a predicate to filter the Files with that selected Folder:
struct FileListView: View {
@FetchRequest(sortDescriptors: [NSSortDescriptor(keyPath: \File.order, ascending: true)], predicate: NSPredicate(format: "folder = %@", appState.selectedFolder), animation: .default)
var fileList: FetchedResults<File>
@EnvironmentObject
var appState: AppState
var body: some View {
List {
ForEach(fileList) {
file in
Text(file.name)
}
}
}
}
However that Predicate does not work as expected and I get the following compilation error:
Cannot use instance member 'appState' within property initializer; property initializers run before 'self' is available