调用performSelectorInBackground:从后台线程 [英] calling performSelectorInBackground: from background thread

查看:207
本文介绍了调用performSelectorInBackground:从后台线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从后台运行的方法调用 performSelectorInBackground:... 的真正效果是什么?我想让它异步运行

What is the real effect of calling performSelectorInBackground:... from a method that is running in the background? I want it to run asynchronously

例如:

- (void) _imageBufferWasUpdated{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    //do something here

    if(shouldContinue){ 
        [self performSelectorInBackground:@selector(_loop) withObject:nil];
    }
    [pool release];
}

_imageBufferWasUpdated将在后台运行,我想异步调用_loop方法背景也因此_imageBufferWasUpdated将很快完成,可能在_loop结束之前)。

_imageBufferWasUpdated will run in the background and I want to call _loop method asynchronously (in the background also so _imageBufferWasUpdated will finish soon, probably before _loop ends).

这是正确的吗?

有更有效(和相对简单)的方法来做这个使用GCD?我会感激它,如果你可以给一些例子如何使用GCD分叉。我想我需要至少3个线程,主线程,后台线程运行_imageBufferWasUpdated和其他后台线程_loop。我是否正确?

Is there a more efficient (and relatively simple) way to do this using GCD? I would appreciate it if you could give some example on how to fork this using GCD. I think I need at least 3 threads, Main thread, background thread for running _imageBufferWasUpdated and other background thread for _loop. Am I correct?

感谢提前
Ignacio

Thanks in advance Ignacio

推荐答案

你在做什么似乎对我很好。 Cocoa可能使用单个后台线程,因此它不应该导致过多的线程创建。

What you are doing seems fine to me. Cocoa probably uses a single background thread, so it should not lead to excessive thread creation.

如果你想要更多的控制,你可以使用NSOperation或GCD。两者都很简单。例如,GCD将是这样

If you want more control, you could use NSOperation or GCD. Both are quite simple. Eg, GCD would be like this

#import <dispatch/dispatch.h>

...

dispatch_async( dispatch_get_global_queue(0,0), ^{
    [self _loop];
}];

这篇关于调用performSelectorInBackground:从后台线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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