当用户阅读邮件时,我使用以下代码更改标签栏上的应用程序徽章编号和徽章。我看到一些无法解释的行为。如果在[UIApplication sharedApplication] .applicationIconBadgeNumber = 1时调用该方法,则applicationIconBadgeNumber将递减为0.但除非我在递减和下一个if之间有睡眠(1)([UIApplication sharedApplication] .applicationIconBadgeNumber> = 1),如果condition的计算结果为True,则DDLogPrint表示"将标签栏标记设置为0,因为appbadge为0"。如果我使用睡眠线,那么事情按预期工作(即标签栏徽章设置为零)。
预计对[UIApplication sharedApplication] .applicationIconBadgeNumber的更改可能需要一些时间吗?
-(void) decrementBadges
{
AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
UITabBarController *myTabBarController = (UITabBarController *)appDelegate.window.rootViewController;
UITabBarItem *tbItem = (UITabBarItem *)[myTabBarController.tabBar.items objectAtIndex:CHAT_TAB_INDEX];
DDLogVerbose(@"In decrement badges with badge number = %ld, tab bar icon = %@, numMessageRead=%ld",(long)[UIApplication sharedApplication].applicationIconBadgeNumber,tbItem.badgeValue, (long)[[NSUserDefaults standardUserDefaults] integerForKey:@"numMsgRead"]) ;
if([UIApplication sharedApplication].applicationIconBadgeNumber>=1)
{
[UIApplication sharedApplication].applicationIconBadgeNumber-- ;
}
//sleep(1) ; //If I uncomment this line, then it works, tab bar badge gets set to nil when method is called with applicationIconBadgeNumber =1
if([UIApplication sharedApplication].applicationIconBadgeNumber>=1)
{
tbItem.badgeValue = [NSString stringWithFormat:@"%ld", (long)[UIApplication sharedApplication].applicationIconBadgeNumber];
DDLogVerbose(@"Setting tab bar badge to %@ because appbadge is %ld",tbItem.badgeValue,(long)[UIApplication sharedApplication].applicationIconBadgeNumber) ;
}
else
{
tbItem.badgeValue = nil;
DDLogVerbose(@"Setting tab bar badge to nil because appbadge is %ld",(long)[UIApplication sharedApplication].applicationIconBadgeNumber) ;
}
}