I want to update app icon badge number by totalling all badge counts. Following is my function. The problem is that count goes out of sync since I'm fetching the count values from APIs and closures make it out of sync. updateBadgeCounts() will be called many times during app usage.
How do I make it work?
extension UIViewController {
func updateBadgeCounts() {
fetchValue1() { (result, error) in
UIApplication.shared.applicationIconBadgeNumber = result!.data!.count!
}
fetchValue2() { (result, error) in
UIApplication.shared.applicationIconBadgeNumber += result!.data!.count!
}
}
}
Calling above func
class MainTabBarController: UITabBarController, UITabBarControllerDelegate {
override func viewDidLoad() {
// Do other stuff...
DispatchQueue.main.async() {
self.updateBadges()
}
}
override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
DispatchQueue.main.async() {
self.updateBadges()
}
}
}