I have a three tableViewController
and they’re managed by the tabBarController
. And each tabBarItem
must be alerted about overdue items like any others apps. In my first tableViewController
I’ve tried to show overdue items count inside of the tabBarItem
’s badgeValue
. These values are comes from the fetchedResultsController
’s fetchedObjects.count
, it well working condition.
func tabBarBadgeUpdater() {
for viewController in (self.tabBarController?.viewControllers)! {
let overdue = self.fetchedResultsController.fetchedObjects?.filter({ (record) -> Bool in
return (record.date?.compare(Date()) != .orderedDescending)
})
print(overdue!.count)
if viewController.tabBarItem.tag == 1 {
if overdue!.count != 0 {
DispatchQueue.main.async(execute: {
viewController.tabBarItem.badgeValue = "\(overdue!.count)"
viewController.tabBarItem.badgeColor = UIColor.init(red: 0.0, green: 0.5, blue: 0.0, alpha: 1.0)
self.tableView.reloadData()
})
} else {
DispatchQueue.main.async(execute: {
viewController.tabBarItem.badgeValue = ""
viewController.tabBarItem.badgeColor = .clear
self.tableView.reloadData()
})
}
}
}
}
But my problem line is deleting them. When I tried to delete last object from the model and it’s still shows value 1.
How to correctly show TabBarItem
's badgeValue
?
I recommend that you implement controllerDidChangeContent
which is a delegate method of your fetchedResultsController
. Everytime your model changes this will be called, and you can query the count and update your badge number.