2012-02-29 39 views
1

我想做使用以下API线程一些动作,太奇怪了选择poiOneBoxSearch还没有被调用,为什么呢?代码上的任何错误?谢谢。奇怪的是,子线程上的IPhone performSelector不起作用。

- (void)poiOneBoxSearch{ 
    [self poiOneBoxSearcWithQueryString:@"coffee" isFinished:YES]; 
} 

- (void)test1{ 
    NSThread* thread = [[NSThread alloc] init]; 
    [self performSelector:@selector(poiOneBoxSearch) 
       onThread:thread 
      withObject:nil 
      waitUntilDone:YES]; 
    [thread release]; 
} 

回答

2

,如果你想使用performSelector方法,你应该阅读以下链接 ,我想你错过了什么

Please Goes Through This Link

如果没有,你可以使用下面的代码。

尝试这个

- (void)test1{ 
[NSThread detachNewThreadSelector:@selector(poiOneBoxSearch) toTarget:self withObject:nil]; 
} 
+0

是的,这种方法的工作原理。 – jianhua 2012-06-11 09:23:38

+0

当许多其他选项没有时,从后台线程调用时,这会起作用。 – 2013-02-20 02:14:21

0

试试这个:

[self performSelectorInBackground:@selector(poiOneBoxSearch) withObject:nil waitUntilDone:YES]; 
0
[self performSelectorInBackground:@selector(poiOneBoxSearch) withObject:nil]; 

- (void) poiOneBoxSearch{ 
      @autoreleasepool { 
    [self poiOneBoxSearcWithQueryString:@"coffee" isFinished:YES]; 
} } 

,你必须记住的最重要的事情是,由于这种方法在给定的选择创建一个线程,选择器必须具有一个自动释放池,就像引用计数的内存环境中的任何其他线程一样。