title: GCD - main vs background thread for updating a UIImageView
body: <p><em>I'm new to GCD and blocks and am easing my way into it.</em> </p>
  
  <p><strong>Background:</strong> I'm working on a lazy loading routine for a UIScrollView using the ALAssetsLibrary. When my UIScrollView loads I populate it with the <code>aspectRatioThumbnails</code> of my ALAssets and then as the user scrolls, I call the routine below to load the <code>fullScreenImage</code> of the ALAsset that is currently being displayed. It seems to work.</p>
  
  <p><em>(if anyone has a better lazy loading routine please post a comment. I've looked at all I could find plus the WWDC video but they seem to deal more with tiling or have much more complexity than I need)</em></p>
  
  <p><strong>My question:</strong> I use a background thread to handle loading the <code>fullScreenImage</code> and when that is done I use the main thread to apply it to the UIImageView. <strong>Do I need to use the main thread?</strong> I've seen that all UIKit updates need to happen on the main thread but I am not sure if that applies to a UIImageView. I was thinking it does, since it is a screen element but then I realized that I simply didn't know.</p>
  
  <pre><code>- (void)loadFullSizeImageByIndex:(int)index
  {
      int arrayIndex = index;
      int tagNumber = index+1;
      ALAsset *asset = [self.assetsArray objectAtIndex:arrayIndex];
  
      __weak typeof(self) weakSelf = self;
  
      dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
          UIImage *tmpImage = [[UIImage alloc] initWithCGImage:asset.defaultRepresentation.fullScreenImage];
  
          if ([weakSelf.scrollView viewWithTag:tagNumber] != nil){
  
              dispatch_async(dispatch_get_main_queue(), ^{
  
                  if ([weakSelf.scrollView viewWithTag:tagNumber]!= nil){
                      UIImageView * tmpImageView = (UIImageView*)[weakSelf.scrollView viewWithTag:tagNumber];
                      tmpImageView.image = tmpImage;
                  }
              });
          }
      });
  }
  </code></pre>