GCD

本文详细介绍了Grand Central Dispatch (GCD) 在iOS开发中的使用方法,包括串行和并发队列的概念、同步和异步任务的区别,以及如何利用GCD进行线程管理来提高应用程序的性能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值