|
(I’m going to clip my code as much as possible to not to frustrate people with 500 lines files) I guess, I finally overcame struggle with Core Data and can normally catch and operate nessesary data in my Views, but I’ve got a situation when I follow MVVM logic, duming all my logic to a separate file. I’ve got my ContentView where all the visuals happening, here’s the excerpt (as a reality check):
DataController's byte-in-byte
And here’s the beginning of my
Down the code I have couple of functions, extensively using fetched data. The work perfectly well, if I move all into one file, but if I call them from
It returns me functions’ results with optional values. I thought that if my
And
And this is where I stuck. I don’t really understand why ‘just’ Help, please! P.S. I also thought about |
|
You can only use @EnvironmentObject, @Environment and @FetchRequest inside a SwiftUI View. You would need to pass your dataController as a parameter to your ViewModel and you would need to create a FetchRequest manually in your ViewModel. |
|
I thought, this is exactly what I did. This is my ViewModel file:
Or, may be I don't understant what does "as a parameter" mean. |
SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th. Sponsor Hacking with Swift and reach the world's largest Swift community! |
|
The property wrappers don't work in your ViewModel. Even when it's a extension of a View. You need to declare them as you would declare them in a normal class.
Then declare a init() with your parameters and do what you need to do to set them up. Unless I got your question completely wrong. |
|
No avail. Even when it's properly declared in
|
|
It seems you can only use FetchResults with @FetchRequest. It seems it doesn't work without it. As you have access to HWS+ you can take a look how to transfer to a ViewModel and how to deal with CoreData. https://www.hackingwithswift.com/plus/ultimate-portfolio-app/bringing-mvvm-into-our-swiftui-project-part-1 |
|
I do have access to HWS+, thanks for the link. I'm just not that far yet in Ultimate Portfolio. I keep issue open and will report the solution when figure it out. |
|
hi, i think what you're trying to do is move the functionality of an you might consider having the view model manage an something like this (remember to
and here's how you make the object respond as the delegate of the fetched results controller ... this is called when changes in Core Data are observed.
hope that helps, DMG |
|
The direct answer on my question is aproximately at 27th minute of beforementioned video. I really struggle summarize it shorter than Mr. Hudson did. If you encounter the same problem, I advice you to watch these two videos. (or just avoid using MVVM in the first place, sometimes it creates chaos) |
|
I rewatched it and it's at 28:20. The short answer is: @FetchRequest doesn't work outside a SwiftUI View :) |
SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.
Sponsor Hacking with Swift and reach the world's largest Swift community!
This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.