I'm trying to filter results to get all characters that are associated with a story. When navigating to the view it filters as it should, but as soon as you select the Add Character option, the app just freezes.
I've found that if I comment out the Query line in the init, and filter out as part of the foreach I don't get the freeze.
Can anyone advise how I should be doing these predicates as it seems init isn't the best place to do it
The views code is
import SwiftUI
import SwiftData
struct CharacterListView: View {
let story : Story
@Query(sort: \Character.name, order: .forward) private var characters: [Character]
var body: some View {
List {
ForEach(characters) { char in
CharacterListCellView(char: char)
}
}
.toolbar {
ToolbarItem {
NavigationLink(destination: CharacterAddView(story: story)) {
Label("Add Character", systemImage: "plus")
}
}
}
.navigationBarTitle("Characters", displayMode: .inline)
}
init(story: Story) {
self.story = story
let id = story.persistentModelID
let predicate = #Predicate<Character> { char in
char.story?.persistentModelID == id
}
_characters = Query(filter: predicate, sort: [SortDescriptor(\.name)] )
}
}
I've narrowed it down to a line in the AddCharacterView file
struct CharacterAddView: View {
@Environment(\.modelContext) private var modelContext
@Environment(\.dismiss) var dismiss
let story : Story
// Character details
@State private var characterName: String = ""
// Images
@State private var showImageMenu = false;
@State private var isShowPhotoLibrary = false
@State private var isShowCamera = false
//@State private var image = UIImage() <-- This line
@State private var imageChanged = false
@State private var isSaving : Bool = false
var body: some View {
}
}
If I do either of the below it works
If I comment out the UIImage variable then I don't get the freeze, but I can't update the image variable
If I comment out the query in init, then it works but doesn't filter and I have to do it as part of the foreach