0

I have an @ObservableObject that I am passing down to a View, using:

TabView()
  .environmentObject(stateManager)

In that view, I'm using:

@EnvironmentObject var stateManager: StateManager

I'm trying to pass some data down one view deeper into a View that will render that data in a list.

var body: some View {
    ZStack {
        Color("bgColor").ignoresSafeArea()
        TabView {
            DataListView(
                items: stateManager.items
            )
                .tabItem {
                    Label("Items", systemImage: "list.bullet")
                }
        }
    }
    .onAppear{
        print("Tab view has appeared!") // <= endless loop
    }
}

In the DataListView (above), I have the following:

var items: [Item]

I have also tried using:

items: $stateManager.items

and

@Binding var items: [Item]

in the tabView and DataListView respectively

For some reason, probably due to my own ignorance, the Tabview renders endlessly.

I've also tried passing the entire state object and other combinations of arguments/parameters with the same result, but I'm bascially guessing at this point.

The items being used are set prior to TabView render, and they are not being mutated at any point, so I'm unsure what's triggering the re-renders.

This issue only occurs when passing down to a "third" level. In other words, if I rendered the list in the TabView, there is no endless, re-render.

Again, there is a 99% probability that I'm not understanding how the data is supposed to flow, so any tips on passing this data down w/o an endless re-render would be most appreciated.

For the record, this is all happening with Xcode 13 beta 4.

Kim
  • 856
  • 1
  • 11
  • 21
  • 1
    Could you share the code of your `DataListView`? That'd be the next place I'd look to see if anything was causing the re-rendering. – ScottM Aug 08 '21 at 12:45
  • 1
    Try moving it "up" one step, declare and initialise `stateManager` in YourApp.swift as a `@StateObject` and then set `.environmentObject(stateManager)` on your `ContentView()` call instead – Joakim Danielson Aug 08 '21 at 12:46

1 Answers1

0

The problem was entirely my bad.

In brief, I had the DataList view written before deciding to put it in a tab view.

And, mistakenly called the TabView within the DataList view before understanding how a TabView is architected.

Thus, when I did add the DataListView into the TabView, TabView got loaded from the DataList view endlessly.

Removing the TabView from the DataList view, stopped the looping.

Totally dumb mistake on my part, but at least I learned something.

Maybe my mistake will help others if they come across this.

Kim
  • 856
  • 1
  • 11
  • 21