|
I know in SwiftUI there are property wrappers to load data from CoreData. But I don't want to distribute the dependencies to the database in all views. So I was wondering how to abstract CoreData? Property Wrapper I can only use one view object. Does anyone of you have an idea how to achive it? |
|
Paul explains in detail how to inject the managed object context into the SwiftUI environment: https://www.hackingwithswift.com/quick-start/swiftui/how-to-configure-core-data-to-work-with-swiftui This seems to be how Apple wants us to use the environment. You just grab the environment variable from whichever view you want. Other views might not know about the variable at all. I would not necessairly call it distributing dependency to all views. Would you? |
|
I know that, but is there another way? I'd like to have "normal" object in my business logic not ManagedObjects |
|
I was under impression that ManagedObjects are part of Core Data stack. Therefore, you would need them regardless if you use SwiftUI or UIKit. Can't think of any other way. Sorry. |
|
It is certainly possible to abstract coreData away. I started down this road in my project, but I reverted to using I'm not here to talk you out of abstracting though! I'll show you what I did and let you make the decision. You can create an
This service can then be injected as an EnvironmentObject that views can use to access the list, make changes, etc. Like I said, there are probably efficiency problems here. I was refetching the entire list any time there was an indication that the list changed. |
|
What I usualy do to avoid manipulating managed objects when it's not convenient —temporary objects, cancelable updates, etc...— is creating beside the
Let's get our old friend
The protocol could be :
The proxy structure :
Finally a
|
|
@Pyroh thank you! However, why include PersonType / what purpose does the protocol serve? |
|
What you are asking for is the idea behind MVVM (Model, View, View-Model) instead of MVC. Shown above you build a model that supports your view structure. Then feed it Coredata model. So, it translates the "business rules" represented by your coredata model into view/display rules used by your views. I suggest reading some on MVVM design. Here is a tutorial on those ideas and Combine: https://www.raywenderlich.com/4161005-mvvm-with-combine-tutorial-for-ios |
|
I was asking myself the same question, this twitter thread has a lot of useful info: https://twitter.com/donnywals/status/1280173379966861313?s=21 |
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 February 9th.
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.