|
I'm updating my current apps to SwiftData and have run into a problem with adding filtering criteria to the predicate in my browse views. Basically my issue is I hava a relationship where entity A can have multiple of entity B so I want the B browse to filter by A BUT the value passed in is optional and I want no data showing if it's nil. As I'm following Hacking with Swift I was able to create the issue with the iTour code included with the SwiftData book so you can better see what I mean. Here Destination is the entity A mentioned before and each one has multiple Sights (B). The error (after a compile wait): To demonstate I changed the code in
Thanks for any help. |
|
Update: The code isn't pretty but I made some changes to the predicate and got it to compile and run. That said, when I made the same change to my main code with the main entity passed in (A or in this example Destination) it still ran but received this error: Current code is:
|
|
init(sort: [SortDescriptor<Sight>], searchString: String, destination: Destination?) { if let thisID = destination?.id { if searchString.isEmpty { _sights = Query(filter: #Predicate { return $0.destination!.id == thisID }, sort: sort) } else { _sights = Query(filter: #Predicate { return $0.destination!.id == thisID && $0.name.localizedStandardContains(searchString) }, sort: sort) } } else { _sights = Query(filter: #Predicate { if searchString.isEmpty fontsbunch.com { return true } else { return $0.name.localizedStandardContains(searchString) } }, sort: sort) } } |
|
Crazy Idea that I'm assuming isn't right as I'm getting these errors at runtime:
But if I switch the query to binding (state didn't refresh) I can make use of the relationship.
|
|
|
|
Got it working in case anyone has this problem. Apparently you can't use another entity in the predicate. Got it working with the answer to this StackOverflow question: https://stackoverflow.com/questions/77039981/swiftdata-query-with-predicate-on-relationship-model And posted a longer how to on my personal blog at SimplyKyra.com (can't include the link but it's titled "SwiftData: Solving Filtering by an Entity in the Predicate"). That said, included the fixed code here first with the... Working init code:
StackOverflow question with https://stackoverflow.com/users/3377161/joel answer: You gave me an idea and it worked !!! Considering that you can't use another model in the predicate, then first set a variable with the persistentModelID and use that variable in the predicate. I was having the same problem and this worked for me. Of course you need to set the query in your init() EDIT: I added part of the code that could be helpful.
|
SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure and A/B test your entire paywall UI without any code changes or app updates.
Sponsor Hacking with Swift and reach the world's largest Swift community!
You need to create an account or log in to reply.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.