I faced a similar problem to the original issue.
I noticed,
if you have any array of objects(class) : [ObjectA]
as an @Published variable in a class: ClassA
if you update any property of the objectA in the array [ObjectA], then the subscribers of @Published [ObjectA] will NOT trigger in ClassA
import Foundation
import Combine
class Object {
var value: Int = 6
init(value: Int) {
self.value = value
}
}
class MyClass {
@Published var objects: [Object] = []
var value: String = "Hello"
var subscriptions = Set<AnyCancellable>()
init(index: Int, value: String) {
self.objects = [Object(value: index)]
self.value = value
$objects
.sink {
print($0.first?.value)
}
.store(in: &subscriptions)
}
}
func changeMemberProperty(_ obj: MyClass) {
if var first = obj.objects.first {
print("Will change member property")
first.value = Int.random(in: 100...400)
}
obj.value += "World"
}
var obj = MyClass(index: 1, value: "Hi")
changeMemberProperty(obj)
changeMemberProperty(obj)
func changeMember(_ obj: MyClass) {
print("Will change member")
obj.objects = [Object(value: Int.random(in: 100...400))]
}
changeMember(obj)
changeMember(obj)
Output:
Optional(1) /* Because of Print-command during initial setup */
Will change member property /* Didn't trigger Print-command */
Will change member property /* Didn't trigger Print-command */
Will change member
Optional(292) /* Due to triggered change: Print-command */
Will change member
Optional(259) /* Due to triggered change: Print-command */