23

I'm not sure if this is an antipattern in this brave new SwiftUI world we live in, but essentially I have an @EnvironmentObject with some basic user information saved in it that my views can call.

I also have an @ObservedObject that owns some data required for this view.

When the view appears, I want to use that @EnvironmentObject to initialize the @ObservedObject:

struct MyCoolView: View { 

    @EnvironmentObject userData: UserData
    @ObservedObject var viewObject: ViewObject = ViewObject(id: self.userData.UID)  

    var body: some View { 
            Text("\(self.viewObject.myCoolProperty)")
    } 
}

Unfortunately I can't call self on the environment variable until after initialization:

"Cannot use instance member 'userData' within property initializer; property initializers run before 'self' is available."

I can see a few possible routes forward, but they all feel like hacks. How should I approach this?

Nerdy Bunz
  • 6,040
  • 10
  • 41
  • 100
snarik
  • 1,035
  • 2
  • 9
  • 15
  • Maybe you can try adding a custom `init` to the struct. – nayem Dec 17 '19 at 03:36
  • I tried that and got a somewhat strange error: `Property wrappers are not yet supported on local properties` Basically its saying I cant create an @ObservedObject in an init method. – snarik Dec 17 '19 at 03:45

2 Answers2

29

Here is the approach (the simplest IMO):

struct MyCoolView: View {
    @EnvironmentObject var userData: UserData

    var body: some View {
        MyCoolInternalView(ViewObject(id: self.userData.UID))
    }
}

struct MyCoolInternalView: View {
    @EnvironmentObject var userData: UserData
    @ObservedObject var viewObject: ViewObject

    init(_ viewObject: ViewObject) {
        self.viewObject = viewObject
    }

    var body: some View {
            Text("\(self.viewObject.myCoolProperty)")
    }
}
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Asperi
  • 228,894
  • 20
  • 464
  • 690
  • 1
    This is perfect. The MyCoolView was actually a child to a 'home' view where I declared the ObservedObject. Thanks! – snarik Dec 19 '19 at 02:01
  • 14
    But what if you want to manipulate the userData inside ViewObject without an entirely new ViewObject being created every time? – BobiSad Feb 22 '20 at 20:05
0

One way to initialize ObservableObject that depends on environment (key or object) is to pass the necessary environment from a parent view via a view init that will itself create the StateObject using wrappedValue: initializer.

Solution
struct FirstScreen: View {
  @Environment(\.fooService) var fooService
  
  var body: some View {
    content
      .sheet(...) {
        SecondScreen(
          // Here we DO have access to environment
          fooService: fooService
        )
      }
  }
}

struct SecondScreen: View {
  @StateObject var viewModel: ViewModel

  init(fooService: FooService) {
    // The ObservableObject initialisation is encapsulated here
    self._viewModel = .init(
      // The autoclosure initializer needs to be used
      wrappedValue: .init(fooService: fooService)
    )
  }
}
Disclosure

Following is a link to my article describing the solution in more detail:

ObservableObject initialisation using Environment

Pavel Holec
  • 1
  • 1
  • 1
  • As I commented in another one of your answers, you **must explicitly** disclose any affiliation you have with sites you link. – Adrian Mole Aug 27 '23 at 04:59