首页 > 其他 > 详细

数据下载-block 形式的异步请求

时间:2015-08-31 10:11:10      阅读:132      评论:0      收藏:0      [点我收藏+]
这里给大家写了一个方法 ,我们使用block 进行数据异步下载 
//block 形式的异步请求
-(void)asyncDownloadWithBlock{
    //菊花
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    //将字符串转化成URL 地址
    NSURL *url = [NSURL URLWithString:self.myURLStr];
    
    //创建一个对象
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    
    //获取主队列
    NSOperationQueue *mainQueue = [NSOperationQueue mainQueue];
    
    NSLog(@"current:%@",[NSThread currentThread]);
    NSLog(@"isMulti:%d",[NSThread isMultiThreaded]);
    NSLog(@"开始下载");
    [NSURLConnection sendAsynchronousRequest:request queue:mainQueue completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        NSLog(@"block 回调,下载完成");
        NSLog(@"current:%@",[NSThread currentThread]);
        NSLog(@"%d",[NSThread isMultiThreaded]);
        
        //取消菊花
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
        
        NSLog(@"%@",response);
        NSHTTPURLResponse *httpRespone = (NSHTTPURLResponse *)response;
        
        //状态码
        if (httpRespone.statusCode != 200) {
            NSLog(@"下载出错");
            
        }
        if (data && !connectionError) {//这里注意判断 数据存在且 连接没有错误
            self.imageView.image = [UIImage imageWithData:data];
            
        }
        else{
            NSLog(@"下载出错");
        }
        
    }];
    NSLog(@"current:%@",[NSThread currentThread]);
    NSLog(@"isMulti:%d",[NSThread isMultiThreaded]);
    NSLog(@"************");
    
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

数据下载-block 形式的异步请求

原文:http://blog.csdn.net/u012701023/article/details/48129133

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!