TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

Updating an EnvironmentObject from a ViewModel

Forums > SwiftUI

Good afternoon,

I'm still getting up to speed in SwiftUI so forgive me if I'm thinking about this totally backwards..

Anyway, I have an applicaiton with a basic create account view. I also have an EnvironmentObject which stores the user property so the child views in the app can access it. However, I'm trying to assign the user I get back from the createAccount request that lives in the createAccountViewModel to the user object that lives in the environmentObject. I'm running into all sorts of headaches, and I haven't been able to find much on stack overflow either.

Has anyone else found a smooth approach to this, or am I thinking about this the wrong way?

Thanks in advance!

2      

I've been running into a similar dilemma where my view model provides data that I want to give to an environment object. Currently, I use my view as a mediator such that it gets data from the view model and gives it to the environment object. This pattern doesn't seem correct though, since I don't think the view should be responsible for managing the flow of data, it should only display it.

I think it makes most sense to provide the view model with a reference to the environment object so the view model can interact with the environment object directly. This would make the view model responsible for managing the flow of data, independent of the view. I haven't found a clean way to do this yet though.

2      

Providing sample code of where you see the problem can help people provide answers.

I setup the data struct at the Application level (xyz.app) (I don't think it matters where it is defined) and then pass it into the views:

import SwiftUI

@main
struct HermesApp: App {

    @Environment(\.scenePhase) private var scenePhase

    let persistenceController = PersistenceController.shared

    var settings = ProjectSettings()

    var body: some Scene {

        WindowGroup {
            HomeView()
                .environment(\.managedObjectContext, persistenceController.container.viewContext)
                .environmentObject(settings)
        }
        .onChange(of: scenePhase) { phase in

            // change in this app's phase - composite of all scenes

            switch phase {

            case .active:
                changedToActive()

            case .background:
                changedToBackground()

            case .inactive:
                changedToInactive()

            default:
                break
            }
        }

Then in the view where I want to access it (read/write/modify):

import SwiftUI
import CoreData

struct ProjectOpenView: View {

    @EnvironmentObject var settings: ProjectSettings

    var body: some View {

        VStack {

            if let project = settings.currentProject {

2      

Hacking with Swift is sponsored by String Catalog.

SPONSORED Get accurate app localizations in minutes using AI. Choose your languages & receive translations for 40+ markets!

Localize My App

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

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.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.