簡體   English   中英

NSOperation-main方法在主線程上運行?

[英]NSOperation - main method runs on the main thread?

為什么我的NSOperation子類在主線程上運行main方法? 我希望它異步運行。

@interface ServiceClient : NSOperation
@end

@implementation ServiceClient

- (void)main
{
   if ([[NSThread currentThread] isEqual:[NSThread mainThread]])
   {
      NSLog(@"Why does this get called on the main thread");
   }
}

@end

開始操作

ServiceClient *serviceClient = [[ServiceClient aloc] init];
[serviceClient start];

編輯:

文檔建議重寫isCuncurrent並返回yes,但是不會調用該方法。

- (BOOL)isConcurrent
{
   return YES;
}

您需要在start方法中設置自己的線程

摘自《 並發編程指南》

開始
(必需)所有並發操作都必須重寫此方法,並用其自己的自定義實現替換默認行為。 要手動執行操作,請調用其start方法。 因此,此方法的實現是操作的起點,並且是設置要在其中執行任務的線程或其他執行環境的位置。 您的實現不得在任何時候調用super。

如果將NSOperation添加到NSOperationQueue它將為您解決這一問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM