总结:
1、创建一个串行队列 执行完任务后需要释放 0 不是在主线程 1 是在主线程 create的队列需要释放2、创建一个全局队列 不需要释放 注 第一个参数指定线程的优先级
GCD的用法 请求数据用分线程 更新UI用主线程
GCD的常见用法
dispatch_queue_t gloablQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(gloablQueue, ^{
NSLog(@"-----网络请求----");
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"---更新UI----");
});
});
dispatch_queue_t gloablQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(gloablQueue, ^{
NSString * url = @"http://api.turetop.com/mobile/BrandList?isapp=true";
NSURL * strUrl = [NSURL URLWithString:url];
NSURLRequest * request = [NSURLRequest requestWithURL:strUrl];
AFHTTPRequestOperation * operation = [[AFHTTPRequestOperation alloc]initWithRequest:request];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation * operation,id object){
NSString * str = operation.responseString;
NSData * data = [str dataUsingEncoding:NSUTF8StringEncoding];
_dictTotal = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
if (_dictTotal != nil) {
_bandArray = [_dictTotal objectForKey:@"brandlist"];
NSLog(@"----品牌数据--%@---",_bandArray);
_classArray = [_dictTotal objectForKey:@"classify"];
dispatch_async(dispatch_get_main_queue(), ^{
[self selectfirstAll];
});
}
} failure:^(AFHTTPRequestOperation * operation,NSError * error){
UIAlertView * alterView = [[UIAlertView alloc]initWithTitle:@"温馨提示" message:@"网络连接失败" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil];
[alterView show];
alterView = nil;
}];
[[NSOperationQueue mainQueue]addOperation:operation];
});
1、创建一个串行队列,第一个参数是C语言格式的字符串指定串行队列的名称 第二个参数目前不支持,写成NULL
//向一个队列上提交一个任务 dispatch_async异步提交 0不是在主线程 异步提交到分线程中的任务是在分线程中执行的
dispatch_async(queue, ^{
NSLog(@"--串行队列-这是在那个线程--%d--",[NSThread isMainThread]);
[NSThread sleepForTimeInterval:5];
NSLog(@"--串行队列--123----");
});
dispatch_async(queue, ^{
[NSThread sleepForTimeInterval:3];
NSLog(@"--串行队列--22222--queue is Main thread %d--",[NSThread isMainThread]);
});
NSLog(@"提交串行队列完毕");
//创建队列需要释放
// dispatch_release(queue);
2、获取一个全局队列 第一个参数指定线程的优先级 第二个参数固定为0 全局队列需要释放
dispatch_queue_t currentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
//异步提交的方式 在分线程中执行
dispatch_async(currentQueue, ^{
[NSThread sleepForTimeInterval:3];
NSLog(@"全局队列11111,currentQueue is main thread %d",[NSThread isMainThread]);
});
dispatch_async(currentQueue, ^{
[NSThread sleepForTimeInterval:2];
NSLog(@"全局队列33333, currentQueue is main thread %d",[NSThread isMainThread]);
});
3、获取主线程的队列,任务在主线程上执行
dispatch_queue_t mainQueue = dispatch_get_main_queue();
dispatch_async(mainQueue, ^{
[NSThread sleepForTimeInterval:1];
NSLog(@"--主线程的队列--%d---",[NSThread isMainThread]);
});
dispatch_async(mainQueue, ^{
[NSThread sleepForTimeInterval:2];
NSLog(@"--主线程的队列1111--%d-----",[NSThread isMainThread]);
});
4、dispatch_sync 同步提交意思是只有等到提交的任务执行完毕,dispatch_sync函数才会返回,dispatch_sync,如果提交到分线程上,那分线程的纸箱也会在分线程上 在主线程中使用dispatch_sync 造成死锁,由于block和viewDidLoad相互等待的原因
dispatch_queue_t mainQueue3 = dispatch_get_main_queue();
dispatch_sync(mainQueue3, ^{
NSLog(@"---使用的是同步提交dispatch_sync----");
});
5、一种等待多个线程结束后通知一种方式
dispatch_queue_t queueGroup = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_group_t group = dispatch_group_create();
dispatch_group_async(group, queueGroup, ^{
[NSThread sleepForTimeInterval:1];
NSLog(@"---group--%d---",[NSThread isMainThread]);
});
dispatch_group_async(group, queueGroup, ^{
[NSThread sleepForTimeInterval:2];
NSLog(@"---group1--%d----",[NSThread isMainThread]);
});
//当多个在group上的任务都结束后才会notify
dispatch_group_notify(group, queueGroup, ^{
NSLog(@"---UpdateUI----");
});