iOS:如何获取后台线程以在主线程上重绘视图 [英] iOS: How to get background thread to redraw view on main thread

查看:254
本文介绍了iOS:如何获取后台线程以在主线程上重绘视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

My application will query a database 50 times and will then react accordingly by adding the right UIImageView to another UIImageView, which I then want the application to display immediately at each loop.

A,经过许多晚上,我无法正常工作.该应用程序将不会立即显示UIImageView.话虽如此,当我在中间循环中放大或缩小时,将出现UIImageViews!我一定想念一些东西...

Alas, after many nights I have failed to get it to work. The application will not immediately display the UIImageView. Having said that, when I zoom in or zoom out during mid loop the UIImageViews will appear! I must be missing something...

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

So far every code works except the last part [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];
}

推荐答案

我发现了解决方案,并真正希望这对某人有帮助...

I discovered the solution and truly hope this will help someone...

        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 

这篇关于iOS:如何获取后台线程以在主线程上重绘视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆