2012-02-29 20 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]; 
} } 

,你必須記住的最重要的事情是,由於這種方法在給定的選擇創建一個線程,選擇器必須具有一個自動釋放池,就像引用計數的內存環境中的任何其他線程一樣。