networkActivityIndi​​catorVisible直到为时已晚才显示 [英] networkActivityIndicatorVisible not showing till its too late

查看:114
本文介绍了networkActivityIndi​​catorVisible直到为时已晚才显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

-(void)buttonClick:(id)sender
{    
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    for(int i=0;i<500000000;i++)
    {
        //Simulates network activity
    }
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}

从理论上讲,屏幕左上角的网络活动微调器应该转on,旋转一段时间,然后关闭......但是,它永远不会出现。

Theoretically, the network activity spinner in the top left corner of the screen should turn on, spin for a while, then turn off...however, it never shows up.

-(void)buttonClick:(id)sender
{
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    for(int i=0;i<500000000;i++)
    {
        //Simulates network activity
    }
    //[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}

当= NO被注释掉时,我注意到程序运行完毕整个for循环,然后在for循环完成后,微调器最终开始旋转。

With "= NO" being commented out, I noticed that the program runs through the entire for loop, then after completion of the for loop, the spinner finally starts spinning.

-(void)viewWillAppear:(BOOL)animated
{
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES
}

-(void)buttonClick:(id)sender
{
    for(int i=0;i<500000000;i++)
    {
        //Simulates network activity
    }
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}

现在,当视图出现时,指示灯开始旋转。当我单击按钮时,它会在循环运行时继续旋转。当for循环结束时,微调器按预期停止。

Now, when the view appears, the indicator starts spinning. When I click the button, it continues spinning while the loop is running. When the for loop ends, the spinner stops as expected.

- = - = - = - = - = - = - = - = - = -

-=-=-=-=-=-=-=-=-=-

我的问题是,为什么旋转器在前两个例子中的for循环之后才开始旋转?

My question is this, why won't the spinner start spinning till after the for loop in the first 2 examples?

- = - = - = - = - = - = - = - = - = -

-=-=-=-=-=-=-=-=-=-

到目前为止我的想法:

关于为什么微调器不会在这个特定的例子中启动...也许它发送消息,但是直到for循环之后才会执行,因为编译的代码如何工作?

In terms of why the spinner won't start in this specific example...Perhaps it sends the message, but doesn't get executed till after the for loop, because of how the compiled code works?

执行此操作的正确方法可能是启用网络活动指示器,然后拍摄另一个执行网络活动的线程,然后该线程将消息发送回主线程完成后(可能还有检索到的数据)

Perhaps the correct way of doing this is to enable the network activity indicator, then shoot off another thread that does the network activity, then that thread shoots a message back to the main thread when it's done (presumably with the data it retrieved as well)

- = - = - = - = - = - = - = - = -

-=-=-=-=-=-=-=-=-

任何建议/答案都表示赞赏。提前致谢。请记住,此应用适用于iPad。

Any advice/answers are appreciated. Thanks in advance. Keep in mind this app is for iPad.

推荐答案

在当前运行循环结束之前,UI不会更新。你的方法当前阻止了主线程,同时做了它的工作,这不会让你的程序有机会到达runloop的末尾。

The UI will not update until the end of the current run loop. Your method currently blocks the main thread whilst doing it's work, which does not give your program a chance to reach the end of the runloop.

所以你需要做的是从主线程中删除长时间运行的任务

So what you need to do is take your long running task off of the main thread

[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{

    for(int i=0;i<500000000;i++)
    {
        //Simulates network activity
    }

    dispatch_async(dispatch_get_main_queue(), ^{
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
    });
});

这篇关于networkActivityIndi​​catorVisible直到为时已晚才显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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