国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設計 > 正文

ios開發(fā)-NSOperation介紹

2019-11-14 19:58:08
字體:
來源:轉載
供稿:網(wǎng)友

簡介:

1、NSOperation是蘋果對GCD的一個面向對象的封裝,是OC的

2、NSOperation同時提供了一些GCD不是特別容易實現(xiàn)的功能

3、將操作添加到隊列,操作會被立即”異步“執(zhí)行

4、NSOperation是個抽象的類,并不具備封裝操作的能力,必須使用它的子類

   1>NSInvocationOperation

  2>NSBlockOperation

  3>自定義類繼承NSOperation,實現(xiàn)內部的方法

代碼實現(xiàn):

示例1:NSInvocationOperation

@interface TBViewController ()

@PRoperty(nonatomic,strong)NSOperation *myQueue;

@end

//懶加載

-(NSOperationQueue *)myQueue

{

  if(_myQueue == nil){

  _myQueue =[ [NSOperationQueue alloc]init];

}

  return _myQueue;

}

 

 

  -(void)touchesBegan:(NSSer *)touches withEvent:(UIEvent *)event

{

  [self opDemo1];

}

-(void)opDemo1

{

  //操作

  NSInvocationOperation *op =[ [NSInvocationOperation alloc]initWithTarget:self selector:@selector(downLoadImage) object:nil];

  //讓操作啟動,如果使用start方法,會在當前線程執(zhí)行操作

 // [op start];

 

  //將操作添加到隊列,操作會立即被“異步”執(zhí)行

  [self.myQueue addOperation:op];

}

//下載圖像

-(void)downLoadImage

{

  NSLog(@"下載圖像:%@",[NSThread currentThread]);

}

 

示例2:NSBlockOperation

//NSOperationQueue  實例化的對象是并發(fā)隊列

-(void)touchesBegan:(NSSer *)touches withEvent:(UIEvent *)event

{

  [self opDemo1];

}

-(void)opDemo2

{

  //操作

  for(int i = 0;i < 10; i ++)

{

  NSBlockOperation *op = [NSBlockOperation blockOperationWithBlock:^{

    NSLog(@"block ===%@",[NSThread currentThread]);

}];

  //添加到隊列  通過運行可以看到是”并發(fā)“隊列

  [self.myQueue addOperation:op];

}  

}

 

示例3:直接添加block

-(void)touchesBegan:(NSSer *)touches withEvent:(UIEvent *)event

{

  [self opDemo3];

}

-(void)opDemo3

{

   for(int i = 0; i < 10; i++)

{

  [self .myQueue addOperationWithBlock:^{

  NSLog(@"===== %@",[NSThread currentThread]);

}];

}

}

示例4:線程間的通訊

-(void)touchesBegan:(NSSer *)touches withEvent:(UIEvent *)event

{

  [self opDemo4];

}

-(void)opDemo4

{

  [self.myQueue addOperationWithBlock:^{

  NSLog(@"下載圖像:%@",[NSThread currentThread]);

  //下載完成需要更新UI,mainQueue  主隊列

  [[NSOperationQueue mainQueue] addOperationWithBlock:^{

  NSLog(@"main %@",[NSThread currentThread]);

}];

}];

}


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 无为县| 白城市| 电白县| 鄂托克旗| 龙里县| 扶沟县| 兰考县| 耒阳市| 山阴县| 思茅市| 磴口县| 武安市| 广水市| 平江县| 丹阳市| 德化县| 吉安市| 崇信县| 基隆市| 交口县| 五大连池市| 金坛市| 海晏县| 阜康市| 梁山县| 阿图什市| 桐梓县| 登封市| 吉隆县| 凉山| 松桃| 灵宝市| 涿鹿县| 阳高县| 容城县| 天峻县| 兴安县| 灯塔市| 扶绥县| 民和| 民和|