I have a ViewModel that has a dependency that has a delegate. I need that delegate to connect with my ViewModel. I have 5 delegates, but only 3 of them work. I tried to recode it but nothing happened.
I have a basic protocol like this.
protocol PrayerRepositoryDelegate: class {
func dataChanged(arr: [Prayer])
}
This protocol is inside my dependencies. When I change something in my array I call that function
var filteredPrayerArray: [Prayer] = [] {
didSet {
delegate?.dataChanged(arr: filteredPrayerArray)
}
}
But I didn't get any response in my ViewModel for the first controller but get that response in other ViewModels. So I assume that delegate in dependencies work.
I connect delegates in init
like this
init(dependency: Dependencies) {
self.dependencies = dependency
super.init()
setupDelegations()
}
private func setupDelegations() {
self.dependencies.prayer.delegate = self
self.dependencies.popesQuotes.delegate = self
self.dependencies.photoQuotes.delegate = self
self.dependencies.divesInMisericordia.delegate = self
self.dependencies.arcibishop.delegate = self
}
3 of them work fine, but another 2 looks like that are not connected. I connect it all as you can see. No works delegates mean that function of the delegate is not called.
Did anybody have the same problem?
OK. I solved it! I just put setupDelegations
into main thread like this:
private func setupDelegations() {
DispatchQueue.main.async {
self.dependencies.prayer.delegate = self
self.dependencies.popesQuotes.delegate = self
self.dependencies.photoQuotes.delegate = self
self.dependencies.divesInMisericordia.delegate = self
self.dependencies.arcibishop.delegate = self
}
}