Let's say that I have a class Employee whose picture property can be observed.
internal class Employee: CustomDebugStringConvertible, Identifiable, ObservableObject {
internal let name: String
internal let role: Role
@Published internal var picture: UIImage?
}
With a class that stores an array of employees. This array might be mutated later so it's a @Published property:
internal class EmployeeStorage: ObservableObject {
@Published internal private(set) var employees: [Employee]
}
Now I have a list of views that uses the employee storage:
struct EmployeeList: View {
@ObservedObject var employeeStorage = EmployeeStorage.sharedInstance
var body: some View {
// Uses employeeStorage.employee property to draw itself
}
}
And the list is used in a content view:
struct ContentView: View {
@ObservedObject var employeeStorage = EmployeeStorage.sharedInstance
var body: some View {
// Uses an EmployeeList value
}
}
Let's say that now the EmployeeStorage object changes mutates employee array: this effectively updates the UI and I can see that the list is updated with the new collection of employees. Problem: what if I want to achieve the same effect when an employee's picture property changes? I thought that this was enough, but in my case (I have a more complex example), the UI is not updated in case that an employee's image changes. How to do it?
ContentViewhas a comment that mentionsEmployeeListbut you haven’t defined anEmployeeListtype. What is anEmployeeList? IsContentViewsupposed to display all the employees in anEmployeeStorage, or just a singleEmployee, or something else? If it’s only supposed to display a singleEmployee, why does it need the entireEmployeeStorage?