ObservableObject Array data not updating

I have two views where I'm trying to get View 2 to run the function in the Observable object and\or update an array so far I've not been able to get it to run or update the array. I'm still learning how to work with ObservableObjects. Thanks!

View1.swift:

  @Published var arrayInt = [6, 6.5]
  @Published var fooString = "Name"
  func appendToArray() {
    fooString = "Boom, updated!"
    arrayInt.append(11)
  }
} 

struct GraphView: View {
@StateObject var graph = graphData()

   var body: some View {
.... some data
   }
}

View2.swift:

struct DetailView: View {
@StateObject var graph = graphData()
   var body: some View {
        ....Some data
                 .onAppear{
          graph.appendToArray()
          graph.arrayInt.append(11)
        }

   }
}

I don't see where you've defined graphData, but for it to be observable it must be a class and have the ObservableObject protocol. Also, arrayInt and fooString must be properties of that class. You then need to instantiate the class and make a single copy of it available to all views. Assuming that you've created your project in a recent version of Xcode, here's an example:

import SwiftUI
@main
struct SwiftUIExampleApp: App {  // the "main" App view created by Xcode
    @StateObject var graphData = GraphData(). // makes a single copy of graphData available 
    var body: some Scene {
        WindowGroup {
            GraphView()  // usually refers to ContentView()
                .environmentObject(graphData). // passes a reference to the single copy of graphData down to GraphView
        }
    }
}

struct GraphView: View {  // Your main view - View1
    @EnvironmentObject var graphData : GraphData
    var body: some View {
        Text(graphData.fooString)
            .padding()
// **** need to invoke DetailView somehow and pass it the reference to graphView i.e. .environmentObject(graphData) ****
    }
}

struct DetailView: View {
    @EnvironmentObject var graphData : GraphData
    var body: some View {
        Text("This is the detail view")
            .onAppear{
                graphData.appendToArray(foo: "OK", num: 21)
                // can also directly address the vars
                graphData.arrayInt.append(11)
                graphData.fooString = "Yippee"
            }
    }
}

import Foundation
class GraphData : ObservableObject {. // this is your "data model" 
    //@Published var arrayInt = [6, 6.5]  // arrayInt contains a real (6.5)
    @Published var arrayInt = [6,7]
    @Published var fooString = "Name"
    func appendToArray(foo: String, num: Int) {
        fooString = foo
        arrayInt.append(num)
      }
}

This should work, but I haven't fully checked - it's still early here in Oz!

Another approach to using GraphData, instead of @StateObject, is to make GraphData a singleton ( static let shared = GraphData() inside the class definition) then use @Published graphData = GraphData.shared

Regards, Michaela

This line

@StateObject var graph = graphData()

initializes a new graph instance in either of your views. They don't communicate with each other.

Better use

@ObservedObject var graph = GraphData.shared

in the views and

class GraphData: ObservableObject {
    static let shared = GraphData()
...
}

in your model class.

ObservableObject Array data not updating
 
 
Q