After having searched the documentation and reading this essentially unanswered question I still don't know how to pass a @Published var from Class A to Class B in SwiftUI.
I'm not sure if I have fundamentally misunderstood something, since in my opinion the following use-case is quite simple: Suppose we have two "ViewModels" or "Controllers"
- LocationManager
- PostManager
The LocationManager has a the current location, while the PostManager -completely independently- is responsible for fetching some posts which it then stores in a property "posts". Let's say this fetching is done based on location, so only posts that have a location in a specific relation to the current location are fetched. By having the two managers separate, all the logic (and source of truth) is neatly organised and not directly in any view. Suppose both these classes live as @StateObject in a view. How can the PostManager always have an up to date version of the location to fetch posts (for example periodically, without any input from any view)?
Of course, as a commenter in the linked question suggests, one could use UserDefaults to accomplish this, but I feel like that is rather a workaround than a state of the art implementation..
Example in code
import Foundation
import SwiftUI
class LocationManager: ObservableObject {
@Published var location: [Double] = []
init() {
// running stuff to keep location up to date
}
}
class PostManager: ObservableObject {
@Published var posts: [LocationPost] = []
init() {
// fetching posts based on location in LocationManager instance <- but how do we always have this information
}
}
class LocationPost {
var text: String = ""
var coordinates: [Double] = []
convenience init(coordinates: [Double] = [], test: String? = nil) {
self.init()
self.coordinates = coordinates
self.text = text
}
}