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]);
}) ;
本文详细介绍了在iOS应用开发中如何利用Grand Central Dispatch (GCD)进行异步操作,确保与UI相关的操作仅在主线程中执行,通过示例展示了如何在后台线程中执行复杂任务并在主线程更新UI。
1550

被折叠的 条评论
为什么被折叠?



