以下にメソッドを定義した場合、メソッドに渡されたブロック (「ハンドラ」) は、によって作成された新しいスレッドで呼び出されNSOperationQueue
ますか? それとも、渡されたときにあったスレッドで呼び出されmethodWithCompletionHandler:
ますか?
-(void)methodWithCompletionHandler:(void (^)(NSString *message))handler
{
// Note: We are currently on thread #1. Calling handler(@"my message") here
// will run on thread #1.
NSBlockOperation* someOp = [NSBlockOperation blockOperationWithBlock: ^{
// do some stuff
}];
[someOp setCompletionBlock:^{
// Note: Now someOp is completing, but it's in thread #2. Does calling the handler
// as below also run in thread #2 or thread #1?
handler(@"Some message.");
}];
NSOperationQueue *queue = [NSOperationQueue new];
[queue addOperation:someOp];
}