Updating all extended views from class gives delay?

I have the weirdest thing, i have a uinavigationgroup and all pages are inherit from a custenViewController, the controller adds a button to the top right with a red badge with an iterator in it. The app could have unlimited pages, so all pages are extended from here. So i can change the iterator over all the pages at once, with the setBadge function for each page.

#import "CustomView.h"


@interface CustomView ()
@end

@implementation CustomView

- (void)viewDidLoad
{
    [super viewDidLoad];

    // add notification icon
    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"icn_notification.png"]
                                                                    style:UIBarButtonItemStylePlain
                                                                   target:self
                                                                   action:@selector(openNotifications:)];
    self.navigationItem.rightBarButtonItem = rightButton;

    // add the badge and set it hidden
    badge = [[UILabel alloc] init];
    badge.frame = CGRectMake(270, -45, ( ( 1 * 8 ) + 14), 22);
    badge.hidden = false;
    badge.text = @"?";
    badge.textColor = [UIColor whiteColor];
    badge.backgroundColor = [UIColor redColor];
    badge.layer.cornerRadius = 11;
    badge.textAlignment = NSTextAlignmentCenter;
    badge.font = [UIFont systemFontOfSize:16];
    badge.shadowColor = [UIColor clearColor];
    badge.shadowOffset = CGSizeMake(0,0);

    self.navigationController.navigationBar.layer.zPosition = -1;
    [self.view addSubview:badge];

    // set the count to the badge
    appDelegate = [[UIApplication sharedApplication] delegate];
    [appDelegate.myData addCustomView:self];
    [self setBadge: appDelegate.myData.badgeCount];
}

-(void)setBadge: (int)count{
    // set the number
    NSString *text = [[NSString alloc] initWithFormat:@"%d", count];
   [badge setText:text];
   [badge setFrame:CGRectMake(270, -45, ( ( [text length] * 8 ) + 14), 22)];
   [badge setHidden: (count == 0)];
}

-(void)openNotifications: (id)sender
{
    // open the subview
    [self.navigationController pushViewController:appDelegate.notificationView animated:YES];
}

@end

and when i wich to update all the badges i do this:

// update badges
for (CustomView *view in customViews) {
    [view setBadge: self.badgeCount];
}

This does work, only the strangest thing is doesnt do it right away, there is a delay of sometimes 1 minute, when i change pages in the app it looks like its goes faster, but i realy dont understand the delay!

Someone please help! This is taking much too long!

Thanks!!

Ok Thanks to David H, i execute it on the main Thread, only what happens when im not on the main? The code doesnt get executed so what am i supposed to do then?

// UI stuff make sure im on the Main Thread
if( [NSThread isMainThread] ){
    // update badges
    for (CustomView *view in customViews) {
        [view setBadge: self.badgeCount];
    }
    // refresh table
    [self.nTable reloadData];
    [self.mTable reloadData];
}else{
    NSLog(@"NOT ON MAIN!!!! %d", [NSThread isMainThread]);
}

This works, only this create's a theoretical situation in which my code isnt executed..... now i am at a loss

Answers


In your "for loop", before it starts, make sure you are on the main thread: assert([NSThread isMainThread]);, if not all kinds of bizarre things will happen. I use these asserts all over my code to insure UI activity never happens on any other thread. Those problems can take weeks to track down, but often crop up if you use lots of blocks and dispatch queues (as I do).

To deal with receiving this on other threads, just wrap the whole thing up in a dispatch block and dispatch it to the main thread. You don't really need check how you got it, the code will always work since it's on the main thread.


Need Your Help

Mocking classes that aren't interfaces

c# unit-testing mocking

I've been writing some providers in c# that inherit from the providerbase class. I've found that it's hard to write tests that use the providers as most mocking frameworks will only allow you to mo...