iOS:如何获得后台线程来重绘主线程上的视图

我的应用程序将查询数据库50次,然后通过将正确的UIImageView添加到另一个UIImageView来做出相应的反应,然后我希望应用程序在每个循环中立即显示。

唉,经过很多晚,我没能把它运行起来。 应用程序不会立即显示UIImageView。 话虽如此,当我放大或缩小中期循环时,UIImageViews将出现! 我肯定错过了什么…

到目前为止,除了最后一部分[UIScrollView performSelector ….

请提前帮助,谢谢。

UIScrollView *firstView; UIImageView *secondView; UIImageView *thirdView; NSOperationQueue *queue; NSInvocationOperation *operation; - (void)viewDidAppear:(BOOL)animated { queue = [NSOperationQueue new]; operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(getData) object:nil]; [queue addOperation:operation]; } - (void) getData { for (i=0 ; i < 50 ; i++) { //All the codes to facilitate XML parsing here [nsXMLParser setDelegate:parser]; [BOOL success = [nsXMLParser parse]; if (success) { if ([parser.currentValue isEqualToString:@"G"]) thirdView.image = greenTick.jpg; [secondView addSubview:thirdView]; } else { NSLog(@"Error parsing document!"); } } [thirdView performSelectorOnMainThread:@selector(setNeedsDisplay) withObject:nil waitUntilDone: YES]; } 

我发现了解决scheme,真心希望这能帮助别人

  if (success) { if ([parser.currentValue isEqualToString:@"G"]) // Make the changes here - start dispatch_queue_t main_queue = dispatch_get_main_queue(); dispatch_async(main_queue, ^{ thirdView.image = greenTick.jpg; [secondView addSubview:thirdView]; }); // Make the changes here - end } else { NSLog(@"Error parsing document!"); } } // Remove performSelectorOnMainThread