Xcode 更新选项卡栏项目延迟';s徽章价值

Xcode 更新选项卡栏项目延迟';s徽章价值,xcode,ios6,uitabbaritem,tabbarcontroller,Xcode,Ios6,Uitabbaritem,Tabbarcontroller,我有一个功能,每5秒运行一次,检查是否有新消息,如果触发了新消息,我将使用以下代码更新选项卡栏项目的徽章值 NSString *chkUrl = @"http://domain.com/script.php"; NSURL *url = [[[NSURL alloc] initWithString:chkUrl] autorelease]; NSError *error = nil; NSStringEncoding encoding; NSString

我有一个功能,每5秒运行一次,检查是否有新消息,如果触发了新消息,我将使用以下代码更新选项卡栏项目的徽章值

    NSString *chkUrl = @"http://domain.com/script.php";
    NSURL *url = [[[NSURL alloc] initWithString:chkUrl] autorelease];
    NSError *error = nil;
    NSStringEncoding encoding;
    NSString *returnHTML = [[NSString alloc] initWithContentsOfURL:url usedEncoding:&encoding error:&error];

    int totalNewMessages = [[returnHTML stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] intValue];
    AppDelegate *mainDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];

    NSLog(@"badge before:%@",[[mainDelegate.tabBarController.viewControllers objectAtIndex:2] tabBarItem].badgeValue);
    [[mainDelegate.tabBarController.viewControllers objectAtIndex:2] tabBarItem].badgeValue = [NSString stringWithFormat:@"%d",totalNewMessages];
    NSLog(@"badge after:%@",[[mainDelegate.tabBarController.viewControllers objectAtIndex:2] tabBarItem].badgeValue);
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,
                                         (unsigned long)NULL), ^(void) {
    [self checkNewMessages];
});
问题是徽章值没有立即更新!也许在第三次或第四次通话时会更新!我做错了什么!或者它是iOS中的一个bug

作为一种解决方法,我在每次更新时重复上述行10次,但它仍然没有更新,因此问题在于更新徽章值的延迟,而不是重新运行更新行

顺便说一句,这个问题在Xcode 4.6模拟器和iOS 6.1的iPhone 5中都发生了。

我发现了问题:)

我每5秒钟运行一次的函数在下面的代码中

    NSString *chkUrl = @"http://domain.com/script.php";
    NSURL *url = [[[NSURL alloc] initWithString:chkUrl] autorelease];
    NSError *error = nil;
    NSStringEncoding encoding;
    NSString *returnHTML = [[NSString alloc] initWithContentsOfURL:url usedEncoding:&encoding error:&error];

    int totalNewMessages = [[returnHTML stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] intValue];
    AppDelegate *mainDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];

    NSLog(@"badge before:%@",[[mainDelegate.tabBarController.viewControllers objectAtIndex:2] tabBarItem].badgeValue);
    [[mainDelegate.tabBarController.viewControllers objectAtIndex:2] tabBarItem].badgeValue = [NSString stringWithFormat:@"%d",totalNewMessages];
    NSLog(@"badge after:%@",[[mainDelegate.tabBarController.viewControllers objectAtIndex:2] tabBarItem].badgeValue);
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,
                                         (unsigned long)NULL), ^(void) {
    [self checkNewMessages];
});
当我把它改成下面,它就像一个符咒

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    dispatch_async(dispatch_get_main_queue(), ^{
        [self checkNewMessages];
    });
});

为什么不设置一个断点来检查
totalNewMessages
值,以便查看传递给您的证卡的内容?你可能有异步调用吗?你首先是如何获得值的?@Alladinian我正在打印badge value的值,在更新之前和之后,它在NSLog中显示正确的值,但在选项卡栏项目badge it self上延迟了几秒钟!你能在代码中设置
totalNewMessages
值的那部分贴出来吗?@Alladinian我已经添加了代码:)谢谢你的关注