Update Delay Tab Icon Icon

I have a function that runs every 5 seconds to check for new messages if a new message is triggered. I am updating the value of the icon in a tab bar item using the code below.

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); 

The problem is that the icon value is not updated immediately! Perhaps in the third fourth call it will be updated! I do nothing wrong! Or is it a bug in iOS!

As work, I repeated the above line 10 times with each update, but again did not update, so the problem is the delay in updating the icon value, it is not a question of restarting the update line!

By the way, this problem occurs both in the Xcode 4.6 simulator and in the iPhone 5 with iOS 6.1

+4
source share
1 answer

I found a problem :)

My function that ran every 5 seconds was inside the code below

 dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, (unsigned long)NULL), ^(void) { [self checkNewMessages]; }); 

When I changed it to below, it worked like a charm!

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

All Articles