Bartłomiej Semańczyk
Bartłomiej Semańczyk

Reputation: 61880

How to debug current queue in dispatch_async?

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

Upvotes: 0

Views: 1049

Answers (2)

P. Pawluś
P. Pawluś

Reputation: 318

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
}

Upvotes: 0

Giordano Scalzo
Giordano Scalzo

Reputation: 6342

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

Upvotes: 1

Related Questions