Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> [課堂實踐與項目]IOS多線程之(3):Grand Central Dispatch(GCD)

[課堂實踐與項目]IOS多線程之(3):Grand Central Dispatch(GCD)

編輯:關於Android編程

//1.使用GCD進行線程處理  ----dispatch_async 使用,避免界面處理耗時的操作時候卡死,先進行線程操作,然後通知主線程更新界面

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
          //線程操作
        NSInteger row = [indexPath row];
        NSString *str = [self.array objectAtIndex:row];
        
        NSURL *url = [NSURL URLWithString:str];
        
        NSData *imageData = [NSData dataWithContentsOfURL:url];
        
        UIImage *image = [[UIImage alloc]initWithData:imageData];
        
        if (image != nil) {
            
            dispatch_async(dispatch_get_main_queue(), ^{
                
                cell.imageView.image = image;
                
                [cell.activityIndicatorView stopAnimating];
                
                //這裡是不能用reloadData的,雖然更新了界面,但是你會發現很多的線程,導致app崩潰。
                //[self.tableView reloadData];
                //更新界面的操作
            });
        }
        
    
    });
     


2.

 

 //2 使用GCD進行線程處理      //dispatch_group_async可以監聽一組事件是否完成,完成之後再通知執行其他的操作
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_group_t group = dispatch_group_create();
    
    
    __block UIImage *image;
    dispatch_group_async(group, queue, ^{
        NSInteger row = [indexPath row];
        NSString *str = [self.array objectAtIndex:row];
        
        NSURL *url = [NSURL URLWithString:str];
        
        NSData *imageData = [NSData dataWithContentsOfURL:url];
        
     image = [[UIImage alloc]initWithData:imageData];
        
        
    });
    //只有上面的操作完成之後,才能進行下面的更新操作。
    
    dispatch_group_notify(group, queue, ^{
    
        cell.imageView.image = image;
        
        [cell.activityIndicatorView stopAnimating];
    
    });
    
    

3.

 

 

    dispatch_queue_t queue = dispatch_queue_create(gcdtest.rongfzh.yc, DISPATCH_QUEUE_CONCURRENT);
    dispatch_async(queue, ^{
        [NSThread sleepForTimeInterval:2];
        NSLog(@dispatch_async1);
    });
    dispatch_async(queue, ^{
        [NSThread sleepForTimeInterval:4];
        NSLog(@dispatch_async2);
    });
    dispatch_barrier_async(queue, ^{
        NSLog(@dispatch_barrier_async);
        [NSThread sleepForTimeInterval:4];

    });
    dispatch_async(queue, ^{
        [NSThread sleepForTimeInterval:1];
        NSLog(@dispatch_async3);
    });

 

2012-09-25 16:20:33.967 gcdTest[45547:11203] dispatch_async1

2012-09-25 16:20:35.967 gcdTest[45547:11303] dispatch_async2

2012-09-25 16:20:35.967 gcdTest[45547:11303] dispatch_barrier_async

2012-09-25 16:20:40.970 gcdTest[45547:11303] dispatch_async3


4.

4、dispatch_apply

 

執行某個代碼片段N次。
dispatch_apply(5, globalQ, ^(size_t index) {
// 執行5次
});

 

  1. 上一頁:
  2. 下一頁:
Copyright © Android教程網 All Rights Reserved