GCD使用(一)执行与UI有关的操作

来源:互联网 发布:方舟史上最强优化 编辑:程序博客网 时间:2024/05/19 15:23

1。使用GCD时,与UI有关的操作只能放在主线程中进行

           dispatch_get_main_queue,

           dispatch_async ( dispatch_queue_t queue, dispatch_block_t block)  异步执行,参数:操作队列,执行块

           dispatch_async_f ( dispatch_queue_t queue,void *context,dispatch_function_t work)  异步执行,参数:操作队列,传入C函数的参数,执行C函数

e.g.     

     dispatch_queue_t  mainQueue = dispatch_get_main_queue();
     dispatch_async(mainQueue, ^(void) {
           UIAlertView *alertView =[[UIAlertView alloc] initWithTitle:@"GCD"  message:@"GCD is amazing!"   delegate:nil  cancelButtonTitle:@"OK"    otherButtonTitles:nil, nil];
           [ alertView show];     

           NSLog(@"Current thread = %@", [NSThread currentThread]);   //此处的操作均是在主线程中进行
           NSLog(@"Main thread = %@", [NSThread mainThread]);  

     }) ;   

0 0