0

This is what I met in one of the projects:

private func updateBadgeView() {

    dispatch_async(dispatch_get_main_queue()) {

        if UIApplication.sharedApplication().applicationIconBadgeNumber > 0 {

            self.newMessagesBadgeView.hidden = false
            self.newMessagesCountLabel.text = String(UIApplication.sharedApplication().applicationIconBadgeNumber)

        } else {
            self.newMessagesBadgeView.hidden = true
        }
    }
}

And I suppose that dispatch_async with that queue is useless here. How can I check the queue here? Do I really need here this block? I need to check if it is the same queue as without block.

The above function is inside subclass of UIViewController and is called in viewDidLoad and viewDidAppear

UPDATE:

I know that because of a bug in ios, it is needed here:

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

    dispatch_async(dispatch_get_main_queue()) {
        self.presentOverlayController(selectViewController)
    }
}
Bartłomiej Semańczyk
  • 59,234
  • 49
  • 233
  • 358
  • Do you want to check if the method is already running on the main queue? See http://stackoverflow.com/questions/5662360/gcd-to-perform-task-in-main-thread. But viewDidLoad etc *are* called on the main queue. – Martin R Sep 20 '16 at 08:25
  • Weird, I never have to do this, what happens if you remove dispatch_async – Khanh Nguyen Sep 20 '16 at 08:26

2 Answers2

1

If it is called in viewDidLoad and viewDidApperar is useless. However you can check with a similar code:

private func updateBadgeView() {

    let block = {
            if UIApplication.sharedApplication().applicationIconBadgeNumber > 0 {

            self.newMessagesBadgeView.hidden = false
            self.newMessagesCountLabel.text = String(UIApplication.sharedApplication().applicationIconBadgeNumber)

        } else {
            self.newMessagesBadgeView.hidden = true
        }
    }

    if ([NSThread isMainThread]) {
        block();
    } else {
        dispatch_async(dispatch_get_main_queue()) {
        block();
        }    
    }
}
Giordano Scalzo
  • 6,312
  • 3
  • 31
  • 31
  • If I debug like you said, it is ok... in both cases (within block and without, there is a main thread). But this is the same for `didSelectRowAtIndexPath:`. There is also main thread for both, but dispatch must be there to make it working? – Bartłomiej Semańczyk Sep 20 '16 at 09:11
  • I suppose that is using the `dispatch_async(dispatch_get_main_queue())` not for switch between threads, but to run the code in the next event in the event loop. Sometimes is needed to handle with animations etc – Giordano Scalzo Sep 20 '16 at 10:36
0

For sure applicationIconBadgeNumber is used in different queue. If you do not use dispatch_async(dispatch_get_main_queue()) you may observe delay while updating your newMessagesCountLabel.

You can use it with Swift 3 syntax now:

DispatchQueue.main.async {
   // your code
}
P. Pawluś
  • 298
  • 6
  • 17