LJ Wilson
LJ Wilson

Reputation: 14427

Update UITabBar Badge Icon from NSOperation thread

I have a background thread (NSOperation) that I use to get new messages for a user. This is called via a timer in the AppDelegate. I have a method that is performed on the MainThread that is supposed to call a class method in my MessagesViewController. This all works (I have log statements to show it is hitting the class method in the MessagesViewController. The method in the MessagesViewController does just one thing:

-(void)updateMessageBadgeNumberWithStringVal:(NSString *)badgeNum {
    [[[[self.tabBarController tabBar] items] objectAtIndex:2] setBadgeValue:badgeNum];
    NSLog(@"updateMessageBadgeNumberWithStringVal Called val= %@", badgeNum);
}

The log statement fires, but the BadgeValue doesn't change. BTW, I am using Storyboard so the TabBarController is not instantiated in the AppDelegate.

Any ideas?

EDIT: In my NSOperation class I am doing this:

[self performSelectorOnMainThread:@selector(updateMessageBadgeNumberWithString:)    
                                              withObject:badgeNum
                                           waitUntilDone:NO];  

That method also lives in my NSoperation class:

-(void)updateMessageBadgeNumberWithString:(NSString *)badgeNum {
    MessagesViewController *mvc = [[MessagesViewController alloc] init];

    [mvc updateMessageBadgeNumberWithStringVal:badgeNum];
    //mvc = nil;

}

So isn't this calling the method that updates the UI on the main thread?

Upvotes: 0

Views: 283

Answers (1)

gschandler
gschandler

Reputation: 3208

Instead of calling updateMessageBadgeNumbeWithStringVal: directly, do this:

[self performSelectorOnMainThread:@selector(updateMessageBadgeNumbeWithStringVal:) withObject:badgeNum waitUntilDone:NO];

You shouldn't update the UI on a background thread, just on the main thread.

Upvotes: 1

Related Questions