I'm trying to implement a search function in SwiftUI with SwiftData. I'm struggling to to query for the recognizedText in my DocumentImage. On the list where I want to filter I have several Document that hold n DocumentImage. I found out that it might not work with the #Predicate macro.
But how could I achieve that?
init(searchTerm: String) {
if !searchTerm.isEmpty {
let predicate = #Predicate<Document> {
$0.title.localizedStandardContains(searchTerm)
}
_documents = Query(filter: predicate)
}
}
@Model
final class Document {
var timestamp = Date.now
var title = Date.now.dateString + "-" + String(localized: "Untitled")
@Relationship(deleteRule: .cascade, inverse: \DocumentImage.document)
var documentImagesStorage: [DocumentImage]? = []
@Relationship(deleteRule: .cascade, inverse: \Tag.document)
var tagsStorage: [Tag]? = []
init(documentImages: [DocumentImage]) {
self.documentImagesStorage = documentImages
}
}
@Model
final class DocumentImage {
var document: Document?
@Attribute(.externalStorage)
var imageData: Data = Data()
var recognizedText: [String]?
var order: Int?
init(imageData: Data, recognizedText: [String], order: Int = 0) {
self.imageData = imageData
self.recognizedText = recognizedText
self.order = order
}
}