Alec
Alec

Reputation: 1706

NSOperationQueue callback on main thread still not updating UI appropriately

So I have a UIViewController which loads data asynchronously using NSOperationQueue. I had the commented out line at first when I was using seed data, but now I have switched to using the actual service. The data gets to this method appropriately, but the UI isn't updating. I then read you need to run UI updates on the main thread, so I changed it to the below code:

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.menuView = (MenuView*)self.view;
    [MenuUIModel loadAndOnSuccess:^(id data, id context) {
        [self.menuView performSelectorOnMainThread:@selector(bindToModel:) withObject:data waitUntilDone:YES];
        //[self.menuView bindToModel:(MenuUIModel*)data];
    } onFail:^(NSString *error, id context) {
        NSLog(@"FAIL with error: %@", error);
    }];
}

Here is the MenuView, though slightly trimmed.

@interface MenuView : UITableView

@property (strong, nonatomic) NSArray *menuItemViews;

// Menu Item View Loading Outlet
@property (strong, nonatomic) IBOutlet MenuItemView *lastLoadedMenuItemView;

- (void) bindToModel:(MenuUIModel*)model;

@end

@implementation MenuView

- (void)bindToModel:(MenuUIModel*)model
{
    if (model) {
        NSMutableArray *mutableMenuItemViews = [NSMutableArray array];
        UINib* inMemoryNib = [UINib nibWithNibName:@"MenuItemView" bundle:nil];
        for (MenuItemUIModel *item in model.Items) {
            [inMemoryNib instantiateWithOwner:self options:nil];
            [self.lastLoadedMenuItemView bindToModel:item];
            [mutableMenuItemViews addObject:self.lastLoadedMenuItemView];
            index++;
        }
        self.menuItemViews = [[NSArray alloc] initWithArray:mutableMenuItemViews];
    }
}

@end

I realize this method of loading nibs is a little different, but the purpose is so I only load the nib and its resources into memory once but instantiate it as necessary. It also works wonderfully with the seed code.

So I thought that running this method on the main thread would work, but apparently not. Another note, I added a test UILabel to MenuView and it updated perfectly when I tested it in this method. So when I call bindToModel inside of my MenuView does that no longer run on the main thread or something?

Upvotes: 0

Views: 647

Answers (1)

Kaelin Colclasure
Kaelin Colclasure

Reputation: 3975

Edit: My first response confused the Cocoa desktop bindings mechanism with the methods you are referring to here as bindings. Cocoa bindings are not implemented at all in Cocoa Touch on iOS, so my previous advice was irrelevant. Sorry about that!

To answer your question, from the code you've shown, yes everything in your -bindToModel: method is running on the main thread. There is no obvious problem with your threading model visible here.

I would suggest adding some NSLog() calls inside your -bindToModel: methods to ensure everything is working as you expect. The output to the console log will include a thread id, so you can tell for certain that things are happening on the thread you intended.

Upvotes: 1

Related Questions