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?