|
I've read this article on creating a custom FetchDescriptor, and I understand the concept, but I'm struggling with the implementation. This code works correctly:
But I need to have quizItems contain the results of a custom FetchDescriptor that applies a filter and limits the returned values to 10. I have created the following DataService struct with a static function for that purpose.
I attempted to put it all together by doing this, and it builds successfully...
...but when I try to run it in the simulator, it crashes when it reaches this view. The error that shows is in the app entry point:
Here's the SwiftData models that are involved. The extra code for Codable conformance is so that I can preload data from a JSON file the first time the app runs.
I have tried a few other things, but am new to SwiftUI development, so most of what I'm doing is just random shots in the dark. Any guidance would be appreciated! |
|
Has nobody used a custom FetchDescriptor in a SwiftData app yet? I'd love to see any working examples even if they are not exactly like what I've tried to do above... Thanks! |
|
SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates. Sponsor Hacking with Swift and reach the world's largest Swift community! |
|
@TheAppApp, Thanks for the suggestion to watch the streamed event. I watched it, and I saw the part you referenced where he showed construction of the query in the init() method. In this particular example, he was only specifying custom predicate and sort descriptors and then feeding them to the @Query macro to generate the results. Based on the article about custom FetchDescriptors, I need to use a FetchDescriptor to be able to limit the results and then use the fetch() method of the model context to retrieve the results of the FetchDescriptor. Trying to put all this together, I added an init() method to my view as follows, but I'm getting an error on the line which calls the fetch() method.
The error says:
I understand that this means that there is a mismatch in data types, but I have no idea how to fix it. I've spent a long time googling this error and reading all the discussions on how to fix it, but the suggestions vary widely depending on the specific situation and data types involved. Any guidance would be greatly appreciated! Thanks! |
|
I've not tried FetchDescriptor in the manner you are doing. I have just used Query(filter:, sort:) etc. Looking at the Apple example at https://developer.apple.com/documentation/swiftdata/filtering-and-sorting-persistent-data I don't see any reference to being able to use a FetchDescriptor, this may be a current limitation of SwiftData. Perhaps others can Chime in. |
|
Thanks again for the reply. My attempt to use FetchDescriptor comes from one of the pages in Paul's SwiftData By Example series called How to Create a Custom FetchDescriptor. My code is taken directly from there, but the problem is that it only shows the code for creating the FetchDescriptor and then calling the fetch() method on the model context, but there is no context shown for how to incorporate that code into a SwiftUI view. Anyone have any thoughts or advice? Thanks in advance! |
|
Mmmmm not quite sure, but I suspect the context Paul used was MVVM. If you take a look at this article it looks similar... https://www.hackingwithswift.com/quick-start/swiftdata/how-to-use-mvvm-to-separate-swiftdata-from-your-views
|
|
Also note that |
|
Just a heads-up, the @Query(descriptor: FetchDescriptor<PersistentModel>) comes with its own initializer. So, it might be a good idea to use that in your init. You could initialize it with _quizItems = @Query(descriptor: FetchDescriptor<PersistentModel>), similar to the approach detailed on Dynamic Plumbing's Vaughan website, right from the start, before you go about setting up the descriptor your way. Trying this approach, as outlined on their site, might just help in eliminating the error you're facing. |
|
@ygeras and @TravisHead, thanks for the replies. I can see that the MVVM example is definitely not what I'm trying to do, so I don't think it helps much. As for the other suggestion, I tried to replace this line
with this line
and also
and I get the following errors:
One of the replies mentioned a "Dynamic Plumbing's Vaughan website." I have no idea what that it, and I was unsuccessful locating it via Google search. Any pointers or links would be greatly appreciated. If it wasn't already obvious, I'm new to SwiftUI in general, so I'm having to learn it at the same time I'm trying to figure out this SwiftData stuff. Thanks for all the help so far! |
|
@TravisHead, I have continued to search for "Dynamic Plumbing's Vaughan website" that you referenced in your reply above, but I cannnot find anything like that. Can you provide a link, please? Thanks! |
|
Hi if still struggling, I may suggest to use this approach.
Do not forget about
To make sample code work. Hope this approach will solve your challenge. the idea is from Mark's book that was recently published. I was thinking to offer similar solution as he has the same approach in CoreData, but at that time, I could not make it work or didn't try hard enough. PS. I am not affiliated with this source in any way. But the book is really great. Just in case the https://www.bigmountainstudio.com |
|
@ygeras, Thanks for the encouragement and the suggestion. It just so happens that I purchased Mark's book the minute it was available Friday, and I'm about halfway through it. I haven't had a chance to try it in my own code yet, but as soon as I saw your suggested code, I recognized it. I'm excited to try it as soon as I get some time (probably Monday). Thanks again! |
SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view 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.