I'm just trying to wrap my head around the Grand Central Dispatch (GCD) and I cannot find the answer to this
So, I know that in general you should not update UIView elements from any thread other than the main thread. So this is illegal:
dispatch_async(workerQueue, ^{ self.view.backgroundColor = [UIColor redColor]; });
But are you allowed to work on the elements of the view before they are added to the view? In other words, is this allowed?
dispatch_async(workerQueue, ^{ UIButton *thisLevelButton = [UIButton buttonWithType:UIButtonTypeCustom]; thisLevelButton.frame = CGRectMake(x, y, BUTTON_WIDTH, BUTTON_HEIGHT); thisLevelButton.tag = kLevelButtonTags + j; int levelState = [[[thisCat objectForKey:[levelNamesInCat objectAtIndex:j]] objectAtIndex:4] intValue]; [thisLevelButton setBackgroundImage:[UIImage imageNamed:@"ccLSButton50lock"]forState:UIControlStateNormal]; thisLevelButton.alpha = 0.5; [thisLevelButton addTarget:self action:@selector(unlockButtonAction:) forControlEvents:UIControlEventTouchUpInside]; dispatch_async(dispatch_get_main_queue(), ^{ [self.view addSubview:thisLevelButton]; } });
source share