线程间通信

  //创建url
    NSURL *url = [NSURL URLWithString:str];
    //创建请求
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    //发送请求
//    NSOperationQueue *queue = [NSOperationQueue mainQueue];
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        NSLog(@"%@",[NSThread currentThread]);
        //JSON解析
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
        if (dict[@"success"]) {
            //回到主线程显示
            dispatch_async(dispatch_get_main_queue(), ^{
                 [MBProgressHUD showError:dict[@"success"]];
            });
         
        }else
        {
            // NSOperationQueue *queue = [[NSOperationQueue alloc] init];如果来到这,会卡死在子线程
            [MBProgressHUD showError:@"!"];
        }

        

posted on 2015-09-24 00:32  菜鸟onTheLoad  阅读(94)  评论(0编辑  收藏  举报