Great, with the latest #Xcode Beta 5 my #SwiftData Queries aren’t working anymore. I’m aware of the issue with the explicit model type information for filter: and sort: … fixed that already.
But why isn’t this simple Query with a #Predicate not working anymore? Any tips?
Maybe it is just me, but I just don't find much "Swiftyness" in #SwiftData. In fact it doesn't feel Swifty at all to me. Well, maybe when that #Predicate with 3 expressions gets back to you with the "unable to resolve blub in time" 🙈
E.g. #Swiftlang has gone (extraordinarily annoying) lengths at making sure that the initialization contract is ensured. No more "half initialized objects" anymore. And then we get this 🤷♀️
The new Foundation/ #SwiftlangPredicates (and its expressions) seem a little weird because they can't be constructed dynamically?
E.g. if I build a #SwiftData app in which I'd let the user construct the query himself (similar to the Finder search), I'd have to hardcode every possible combination of search fields?
Did someone find a workaround for this?
@helge@kharrison Haven’t tried it but if you generate a predicate for each case in your enum - then could you could loop over them and AND or OR them together like this?
var predicate = query.first.predicate
for subQuery in query.dropFirst {
predicate = #Predicate { predicate && subQuery.predicate }
}
So are Compound Predicates just not a thing with the new #Predicate? That makes them… pretty tough to work with for anything beyond example projects. #SwiftData
I just filed a whole bunch of feedback reports for Apple regarding SwiftData. If you see any of these and want the same, please file your own report asking for it – every feedback counts, particularly now as we're still in the early betas.
FB12416784: Please add compound predicate support with #Predicate
FB12416816: Please expose NSMergePolicy for when SwiftData syncs with CloudKit
FB12416840: Please provide live query support with an equivalent of NSFetchedResultsController
Made great progress with my Wine app today converting to #SwiftData once I figured out how to create a #Predicate to match criteria on related properties. Works really well. And easy to understand. I am back to loving SwiftData again.
I've been playing around with #SwiftData predicates for hours and I must be doing something very wrong. They work, but only for the most basic of stuff.
The first one works. The second one and other variants of it results in me getting back nothing.