線程操作-將一個函數在主線程執行的四種方法


 

  • GCD方法,通過向主線程隊列發送一個block塊,使block里的方法可以在主線程中執行。
dispatch_async(dispatch_get_main_queue(), ^{ //需要執行的方法 });
  • NSOperation 方法
NSOperationQueue *mainQueue = [NSOperationQueue mainQueue]; //主隊列 NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{ //需要執行的方法 }]; [mainQueue addOperation:operation];
  • NSThread 方法
[self performSelector:@selector(method) onThread:[NSThread mainThread] withObject:nil waitUntilDone:YES modes:nil]; [self performSelectorOnMainThread:@selector(method) withObject:nil waitUntilDone:YES]; [[NSThread mainThread] performSelector:@selector(method) withObject:nil];
  • RunLoop方法
[[NSRunLoop mainRunLoop] performSelector:@selector(method) withObject:nil]; 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM