iPhone:如何使用performSelector:onThread:withObject:waitUntilDone:方法? [英] iPhone: how to use performSelector:onThread:withObject:waitUntilDone: method?

查看:213
本文介绍了iPhone:如何使用performSelector:onThread:withObject:waitUntilDone:方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用单独的线程来处理某些API。

I am trying to use a separate thread for working with some API.

问题是我无法使用 performSelector: onThread:withObject:waitUntilDone:带有我为此实例化的线程的方法。

The problem is that I am not able to use performSelector:onThread:withObject:waitUntilDone: method with a thread that I' instantiated for this.

我的代码:

@interface MyObject : NSObject {
  NSThread *_myThread;
}
@property(nonatomic, retain) NSThread *myThread;
@end

@implementation MyObject
@synthesize myThread = _myThread;
- (NSThread *)myThread {
  if (_myThread == nil) {
    NSThread *myThreadTemp = [[NSThread alloc] init];
    [myThreadTemp start];
    self. myThread = myThreadTemp;
    [myThreadTemp release];
  }
  return _myThread;
}

- (id)init {
  if (self = [super init]) {
    [self performSelector:@selector(privateInit:) onThread:[self myThread] withObject:nil waitUntilDone:NO];
  }
  return self;
}
- (void)privateInit:(id)object {
  NSLog(@"MyObject - privateInit start");
}

- (void)dealloc {
  [_myThread release];
  _myThread = nil;
  [super dealloc];
}
@end

MyObject - privateInit开始永远不会打印。

我缺少什么?

"MyObject - privateInit start" is never printed.
What am I missing?

我试图用目标来实例化线程选择器,试图等待方法执行完成( waitUntilDone:YES )。

没有任何帮助。

I tried to instantiate the thread with target and selector, tried to wait for method execution completion (waitUntilDone:YES).
Nothing helps.

UPDATE:

我不需要这种多线程来将昂贵的操作分离到另一个线程。

在这种情况下我可以使用在几个答案中提到的performSelectorInBackground

这个单独线程的主要原因是需要从一个单独的线程执行API中的所有操作(TTS by Loquendo)。

意味着我必须创建一个TTS对象的实例,并始终从同一个线程调用该对象的方法。

UPDATE:
I don't need this multithreading for separating costly operations to another thread.
In this case I could use the performSelectorInBackground as mentioned in few answers.
The main reason for this separate thread is the need to perform all the actions in the API (TTS by Loquendo) from one single thread.
Meaning that I have to create an instance of the TTS object and call methods on that object from the same thread all the time.

推荐答案

我找到了答案!

为了保持线程,需要另外一块鳕鱼e:

In order to keep the thread up, there is a need in additional piece of code:

- (void)threadMain:(id)data {
    NSAutoreleasePool *pool = [NSAutoreleasePool new];

    NSRunLoop *runloop = [NSRunLoop currentRunLoop];
    [runloop addPort:[NSMachPort port] forMode:NSDefaultRunLoopMode];

    while (isAlive) { // 'isAlive' is a variable that is used to control the thread existence...
        [runloop runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
    }

    [pool release];
}


下一行:

NSThread *myThreadTemp = [[NSThread alloc] init];

应该替换为:

NSThread *myThreadTemp = [[NSThread alloc] initWithTarget:self selector:@selector(threadMain:) object:nil];

编辑:正如我在这里的人所建议的那样,我添加了一些代码行(NSAutoreleasePool,addPort方法和'isAlive'布尔值)。

EDIT: As was suggested by few people here I've added few lines of code (NSAutoreleasePool, addPort method and 'isAlive' boolean).

这篇关于iPhone:如何使用performSelector:onThread:withObject:waitUntilDone:方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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