I have a class (WatchlistClass) that conforms to the "ObservableObject" protocol and that holds a @Published var (watchlist). The var, in turn, holds a whole array of stocks and some information ([StockInformation]), which is supposed to update my view (WatchlistView) with the current stock prices and stuff. That part works perfectly fine, but the class should access the @StateObject in the view to change the data. Accessing it directly in the class doesn't work because it doesn't read the data from the @StateObject. I tried to access the @StateObject in the view directly but that also creates a new instance of the class that has an empty array. Using "static" on a @Published var throws an error. I can't, for the life of me, figure out how to access a @StateObject directly inside the view to read and modify the data that it holds. Any help would be appreciated.
class WatchlistClass: ObservableObject {
static let shared = WatchlistClass()
@Published var watchlist: [StockInformation] = []
struct StockInformation: Identifiable {
...
}
func WatchlistTasksGetFundamentalsDelegate(string: String) {
...
DispatchQueue.main.async {
self.watchlist.append(stockInformation) // This works and updates the view as expected
}
...
}
private let timer = Timer.scheduledTimer(withTimeInterval: 4.0, repeats: true) { _ in
if self.watchlist.isEmpty == false { // This does not work
...
}
}
struct WatchlistView: View {
@StateObject var watchlistClass = WatchlistClass()
...
}