2016-08-13 17 views
1

を実行していない:waitUntilDoneはperformSelector:onThread:withObject:私は機能を持っている私の機能

-(void)doTask{ 
NSLog(@"do task start."); 
... 
} 

私は別のスレッド上でこの機能を実行したい、これは私が試したものです:

NSThread *workerThread = [[NSThread alloc] init]; 
[workerThread start]; 

[self performSelector:@selector(doTask) 
      onThread:workerThread 
      withObject:nil 
     waitUntilDone:NO]; 

実行すると、doTask機能は実行されません。どうして?

+1

多分[このスレッド](http://stackoverflow.com/q/2584394/6541007)が役立ちます。 – OOPer

答えて

0

スレッドが起動時に実行するselectorを定義します。

NSThread* myThread = [[NSThread alloc] initWithTarget:self 
              selector:@selector(myThreadMainMethod:) 
               object:nil]; 
[myThread start]; // Actually create the thread 

そして:

- (void)myThreadMainMethod:(id)object { 
    @autoreleasepool { 
     NSLog(@"do task start; object = %@", object); 
    } 
} 

Threading Programming Guideを参照してください。


グランドセントラルディスパッチまたはオペレーションキューを使用してください。スレッディングがはるかに簡単です。 同時処理プログラミングガイドのMigrating Away from Threadsセクションを参照してください。

+0

はい、これはうまくいきますが、私のコードでどうすればいいのですか?実際には、私は前に、このコードを使用していた複雑な理由のために、私は最初にスレッドを作成し、開始スレッドでセレクタを実行する必要があります。出来ますか? –

+0

[実行ループの設定](https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/Multithreading/CreatingThreads/CreatingThreads.html#//apple_ref/doc/uid/10000057i-CH15- SW25)と[実行ループはいつ使うのですか?](https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/Multithreading/RunLoopManagement/RunLoopManagement.html#//apple_ref/doc/uid/ 10000057i-CH16-SW24)。 – Rob

関連する問題