GCD

GCD Demo

  • (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    //同步 和 異步
    //具備開闢線程的能力

    //GCD 棧
    //隊列 和 任務

    //A B

    //串行隊列
    //dispatch_queue_create(const char *label, dispatch_queue_attr_t attr)

    //併發隊列
    //dispatch_get_global_queue(long identifier, unsigned long flags)

    //主隊列
    //dispatch_get_main_queue()

    //同步任務 基本不適用 不會創建線程
    //dispatch_sync(dispatch_queue_t queue, <#^(void)block#>)

    //異步任務
    //dispatch_async(<#dispatch_queue_t queue#>, <#^(void)block#>)
    if 0
    //同步串行
    dispatch_queue_t queue = dispatch_queue_create(“abc”, nil);

    //A
    dispatch_sync(queue, ^{
    NSLog(@”111%@”,[NSThread currentThread]);
    });

    //B
    dispatch_sync(queue, ^{
    NSLog(@”222%@”,[NSThread currentThread]);
    });

    //同步併發
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

    //A
    dispatch_sync(queue, ^{
    NSLog(@”111%@”,[NSThread currentThread]);
    });

    //B
    dispatch_sync(queue, ^{
    NSLog(@”222%@”,[NSThread currentThread]);
    });

endif

//異步串行  創建一個線程 執行多個任務
dispatch_queue_t queue1 = dispatch_queue_create("abc", nil);

dispatch_async(queue1, ^{
    NSInteger i = 0;
    while (i < 100) {
        NSLog(@"%ld",i++);
    }
    NSLog(@"111%@",[NSThread currentThread]);
});

dispatch_async(queue1, ^{
    NSInteger i = 999;
    while (i < 2000) {
        NSLog(@"%ld",i++);
    }
    NSLog(@"222%@",[NSThread currentThread]);
});

//異步併發  創建多個線程 執行多個任務
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

dispatch_async(queue, ^{
    NSInteger i = 0;
    while (i < 100) {
        NSLog(@"%ld",i++);
    }
    NSLog(@"111%@",[NSThread currentThread]);
});

dispatch_async(queue, ^{
    NSInteger i = 999;
    while (i < 2000) {
        NSLog(@"%ld",i++);
    }
    NSLog(@"222%@",[NSThread currentThread]);
});

}

  • (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    }

//單例
- (IBAction)clcik:(id)sender {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@”https://ss2.baidu.com/6ONYsjip0QIZ8tyhnq/it/u=3200710946,482481474&fm=80“]];
//回到主線程 刷新ui
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@”%d”,[NSThread isMainThread]);
self.imageView.image = [UIImage imageWithData:data];
});
});
}

寫一個單例

+(TQPerson *)sharedPerson
{
static TQPerson *p = nil;

static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
    p = [[TQPerson alloc] init];
});

return p;

}

GCD Group

  • (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

if 0
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
dispatch_group_t group = dispatch_group_create();

//AF
//A  4
dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

// dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(4 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
// NSLog(@”A 任務完成”);
// });
sleep(4);
NSLog(@”A 任務完成”);
});

//B  6
dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

// dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(6 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
// NSLog(@”B 任務完成”);
// });
sleep(6);
NSLog(@”B 任務完成”);
});

dispatch_group_notify(group, dispatch_get_main_queue(), ^{
   [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
});

//
//dispatch_group_enter(dispatch_group_t group)  //retain +1
//dispatch_group_leave(dispatch_group_t group)  //release -1
//0

endif

[self testGroup];

}

-(void)testGroup
{
dispatch_group_t group = dispatch_group_create();

[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];

dispatch_group_enter(group); //+1

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(4 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    dispatch_group_leave(group); //-1
    NSLog(@"123");
});

dispatch_group_enter(group);

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(6 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    dispatch_group_leave(group); //-1
    NSLog(@"456");
});

dispatch_group_notify(group, dispatch_get_main_queue(), ^{
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
});

//AF

}

發佈了83 篇原創文章 · 獲贊 0 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章