I am working in Xcode 15 (beta) migrating to SwiftData and am having a hard time figuring out how to form a search predicate for my one to many model. The desired result is to return a query that returns only the articles and sections where the article "search" field contains the string a user is searching for.
Here is my current model:
@Model
class SectionsSD {
@Attribute(.unique) var id: String
var section: String
var rank: String
var toArticles: [ArticlesSD]?
init(id:String, section: String, rank: String) {
self.id = id
self.section = section
self.rank = rank
}
}
@Model
class ArticlesSD {
var id: String
var title: String
var summary: String
var search: String
var section: String
var body: String
@Relationship(inverse: \SectionsSD.toArticles) var toSection: SectionsSD?
init(id: String, title: String, summary: String, search: String, section: String, body: String) {
self.id = id
self.title = title
self.summary = summary
self.search = search
self.section = section
self.body = body
}
}
In CoreData I was able to do the following in my code to form and pass the search predicate ("filter" being the user input search text, "SectionsEntity" being my old CoreData model):
_fetchRequest = FetchRequest<SectionsEntity>(sortDescriptors: [SortDescriptor(\.rank)], predicate: NSPredicate(format: "toArticles.search CONTAINS %@", filter))
I can't find any examples or info anywhere that explains how to form a predicate in SwiftData to achieve the same search results. I can't seem to find a way to represent toArticles.search properly assuming the capability is there. Here is what I've tried but Xcode complains about the $0.toArticles?.contains(filter) with errors about "Cannot convert value of type 'Bool?' to closure result type 'Bool'" and "Instance method 'contains' requires the types 'ArticlesSD' and 'String.Element' (aka 'Character') be equivalent"
let searchPredicate = #Predicate<SectionsSD> {
$0.toArticles?.contains(filter)
}
_sectionsSD = Query(filter: searchPredicate)
I've tried $0.toArticles?.search.contains(filter) but Xcode can't seem to find its way there like it did using CoreData
Any suggestions and examples on how to form a predicate in this use case would be appreciated.