异步函数执行? [英] Asynchronous function execution?

查看:136
本文介绍了异步函数执行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的iOS应用中,我执行以下操作。

in my iOS app I do the following.

viewDidAppear(){

   // Load a spinner in a view on the top
   [DSBezelActivityView newActivityViewForView:self.view]; 
   // Execute code that require 3 seconds
   ...
   // Stop the spinner
   [DSBezelActivityView removeViewAnimated:YES];
}

问题是spinner没有出现,因为cpu是努力工作(类似的东西)。这就像开始和停止之间的代码优先于视图的渲染。

The problem is that the spinner doesn't appear, because the the cpu is working hard (something similar). It's like that the code betweek the start and stop has precedence on the rendering of the view.

我希望找到一种方法来有效地展示微调器的开始,没有使用计时器来延迟代码执行。

I would love to find a way to show effectively the start of the spinner, without using a timer to delay the code execution.

谢谢

推荐答案

如果您有类似的方法

-(void) showSpinner:(UIView*)view {
    dispatch_async(dispatch_get_main_queue(), ^{
        [DSBezelActivityView newActivityViewForView:view];
    });
}

有几种方法可以从不同的线程调用它。从以下选项中选择一个:

there are several ways to call it from a different thread. Choose one from the following:

[NSThread detachNewThreadSelector:@selector(showSpinner:) toTarget:self withObject:self.view];
// or 
[self performSelectorInBackground:@selector(showSpinner:) withObject:self.view];
// or 
NSInvocationOperation *invOperation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(showSpinner:) object:self.view];
NSOperationQueue *opQueue = [[NSOperationQueue alloc] init];
[opQueue addOperation:invOperation];
// or 
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    [self showSpinner:self.view];
});

Alt +点击了解详情。

Alt + click for details.

这篇关于异步函数执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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