Cookie Consent by Free Privacy Policy Generator 📌 SwiftData: Solving Filtering by an Entity in the Predicate


✅ SwiftData: Solving Filtering by an Entity in the Predicate


💡 Newskategorie: Programmierung
🔗 Quelle: dev.to

I just wanted to start out by saying that this is an abridged version of my original published version from December 15, 2023 that you can find on my website here.

When moving my apps over from CoreData to SwiftData I came across a problem where I wasn't able to filter my query by another entity within its predicate. Whenever I did and tried to build the project it kept compiling and compiling and would eventually fail so the time wasted waiting while trying to solve the issue was another aggravation on top of it all.

To demonstrate the problem I took the iTour example project from Hacking with Swift and altered it a bit to showcase my problem and, eventually, the solution.

In his app there are two main data models. You have destinations you can visit and each one has a list of sights you can see at that destination. So for example if you were to visit Alberta (the destination) you could visit many sights while there like: Banff, Lake Louise, Royal Tyrrell Museum, and West Edmonton Mall. While a Destination can include multiple Sights each Sight can only belong to one Destination. His example didn't show my issue but I altered it to include a list of Sights that needed to be filtered by the id of a Destination thus recreating my problem.

Image showing one of the errors

The problem, once solved, was very simple. I was filtering the Sight data thus I could only filter by the data model Sight and no other ones. My problem came when I introduced the Destination. To get around this I took the persistentModelID from the Destination before entering into the Predicate to filter by. That worked!

Image shows the filtered data in two ways.

The working code, in case you want to use it towards your solution, is:

init(sort: [SortDescriptor<Sight>], searchString: String, destination: Destination?) {
  // Make a point of grabbing 
  if let thisID = destination?.persistentModelID {
    _sights = Query(filter: #Predicate<Sight> {
      if searchString.isEmpty {
        return $0.destination?.persistentModelID == thisID
      } else {
        return $0.destination?.persistentModelID == thisID && $0.name.localizedStandardContains(searchString)
      }
    }, sort: sort)
  } else {
    // if id is nil than I don't want ANYTHING that said you could instead return $0.name.localizedStandardContains(searchString)
    _sights = Query(filter: #Predicate<Sight> { currSight in
      return false
    }, sort: sort)
  }
}

And with that the issue was solved! If you want more information about the problem, the example, the building issues, or the solution itself you can check out my SimplyKyra website where I published the longer and original version of this post back in December of 2023.

Hope you have a great day!

...

✅ SwiftData: Solving Filtering by an Entity in the Predicate


📈 96.07 Punkte

✅ What is URL filtering? Web filtering explained


📈 30.13 Punkte

✅ Wir basteln mit SwiftUI, SwiftData und Co. die Fardal App


📈 29.28 Punkte

✅ Before WWDC 2024: The Future Potential and Real Challenges of SwiftData


📈 29.28 Punkte

✅ Making a New Item in SwiftData go Directly to Edit View, or "How to Get Things Exactly Backwards"


📈 29.28 Punkte

✅ How to Dynamically Construct Complex Predicates for SwiftData


📈 29.28 Punkte

✅ How To Handle Optional Values in SwiftData Predicates


📈 29.28 Punkte

✅ JavaScript: Separate Predicate Functions


📈 28.56 Punkte

✅ Swift Predicate: Usage, Composition, and Considerations


📈 28.56 Punkte

✅ slice-deque Crate up to 2021-02-19 on Rust Predicate SliceDeque::drain_filter denial of service


📈 28.56 Punkte

✅ Bolton’s Stated Predicate for War With Iran Doesn’t Work


📈 28.56 Punkte

✅ Bolton’s Stated Predicate for War With Iran Doesn’t Work


📈 28.56 Punkte

✅ Backupwordpress Plugin Predicate.php bkpwp_plugin_path unknown vulnerability


📈 28.56 Punkte

✅ You're Not Solving the Problem You Think You're Solving


📈 27.18 Punkte

✅ Solving a VM-based CTF challenge without solving it properly - gynvael.coldwind//vx.log


📈 27.18 Punkte

✅ You're Not Solving the Problem You Think You're Solving | AI Show


📈 27.18 Punkte

✅ Solving the puzzle of entity resolution with an easy-to-use API


📈 23.17 Punkte

✅ Announcing Entity Framework Core 3.1 and Entity Framework 6.4


📈 19.15 Punkte

✅ Announcing Entity Framework Core 3.0 and Entity Framework 6.3 General Availability


📈 19.15 Punkte

✅ Entity Disambiguation via Fusion Entity Decoding


📈 19.15 Punkte

✅ Release Candidate builds of Entity Framework Core 3.0 and Entity Framework 6.3 are now available


📈 19.15 Punkte

✅ CVE-2013-6461 | Nokogiri 1.5.x/1.6.x XML Entity xml entity expansion (XFDB-90059 / BID-64513)


📈 19.15 Punkte

✅ Announcing Entity Framework Core 3.0 Preview 9 and Entity Framework 6.3 Preview 9


📈 19.15 Punkte

✅ libxml2 XML Internal Entity Expansion ENTITY memory corruption


📈 19.15 Punkte

✅ Announcing Entity Framework Core 3.0 Preview 8 and Entity Framework 6.3 Preview 8


📈 19.15 Punkte

✅ Config File Provider Plugin up to 3.7.0 on Jenkins XML External Entity xml external entity reference


📈 19.15 Punkte

✅ Announcing Entity Framework Core 3.0 Preview 7 and Entity Framework 6.3 Preview 7


📈 19.15 Punkte

✅ Announcing Entity Framework Core 3.0 Preview 6 and Entity Framework 6.3 Preview 6


📈 19.15 Punkte

✅ CASE Suite up to 3.10 Parameter Entity XML External Entity


📈 19.15 Punkte

✅ Entity API module up to 7.x-1.2 on Drupal Entity Wrapper Access API Comment privilege escalation


📈 19.15 Punkte

✅ Entity API module up to 7.x-1.2 on Drupal Entity Wrapper Access API privilege escalation


📈 19.15 Punkte

✅ Brisk Menu Gets Basic Keyword Filtering in v0.3.0, Enables Internationalization


📈 15.07 Punkte

✅ Apache Thrift, Jinja, JMESPath: Qrator Filtering Network Configuration Delivery


📈 15.07 Punkte











matomo

Datei nicht gefunden!