NSOperation封装- 多任务下载

24 篇文章 0 订阅
21 篇文章 0 订阅
#import <Foundation/Foundation.h>



/**
 完成回调
 */

#warning mark - block在主线程中创建 回调就必须用主线程

typedef void(^OperationDidFinishBlock)(void);


/**
 进度回调
 */
typedef void(^OPerationDidRecieveProgressBlock)(float progress);

@interface MyOperation : NSOperation




/**
 *  NSURL构造方法
 */
- (id)initWithUrl:(NSURL *)url;

/**
 *  完成回调
 *
 *  @param finishblock <#finishblock description#>
 */
- (void)setOPerationDidFinish:(OperationDidFinishBlock)finishBlock;


/**
 *  进度回调
 */

- (void)setOperationDIdFinishRecieveProgressBlock:(OPerationDidRecieveProgressBlock)progress;

@end

#import "MyOperation.h"

@interface MyOperation ()<NSURLConnectionDataDelegate>
{
    NSURL *_url;
    
    //是否停止下载
    BOOL _isEndRequest;
    
    //完成回调
    OperationDidFinishBlock _finishBlock;
    
    //进度回调
    OPerationDidRecieveProgressBlock _progressBlock;
    
    //文件总大小
    long long _filesize;
    
}

@property(nonatomic, strong)NSMutableData *downloadData;

@end

@implementation MyOperation





- (id)initWithUrl:(NSURL *)url
{
    if (self = [super init]) {
        _url = url;
    }
    return self;
}

//子线程
- (void)main
{
    NSLog(@"main 方法自动调用%d",[NSThread isMainThread]);
    
    [self startRequest];//发送异步请求
    
    while (!_isEndRequest) {
        
        //主线程runloop 默认是启动的 分线程要手动启动
        
        [[NSRunLoop currentRunLoop]runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
        
    }
    
    
    NSLog(@"main方法完成 任务取消");
}
//发送请求
- (void)startRequest
{
    [NSURLConnection connectionWithRequest:[NSURLRequest requestWithURL:_url] delegate:self];
}

#pragma mark - getter方法

- (NSMutableData *)downloadData
{
    if (!_downloadData) {
        _downloadData = [NSMutableData data];
    }
    return _downloadData;
}


#pragma mark - setter方法

- (void)setOPerationDidFinish:(OperationDidFinishBlock)finishBlock
{
    _finishBlock = finishBlock;
}


- (void)setOperationDIdFinishRecieveProgressBlock:(OPerationDidRecieveProgressBlock)progress
{
    _progressBlock = progress;
}

#pragma mark - NSURLConnectionDataDelegate代理方法

/**
 *  响应头
 *
 *  @param connection <#connection description#>
 *  @param response   <#response description#>
 */
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    NSLog(@"收到服务器响应");
    
    self.downloadData.length = 0;
    
    //文件大小
    _filesize = [response expectedContentLength];
}

/**
 *  接受数据
 *
 *  @param connection <#connection description#>
 *  @param data       <#data description#>
 */
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    //接受数据
    [self.downloadData appendData:data];
    
    //计算进度
    float progress = (float)self.downloadData.length/_filesize;
    
    //进度回调
    dispatch_async(dispatch_get_main_queue(), ^{
        if (_progressBlock) {
            _progressBlock(progress);
        }
    });

    
}

/**
 *  连接完成
 *
 *  @param connection <#connection description#>
 */
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSLog(@"请求完成");
    dispatch_async(dispatch_get_main_queue(), ^{
        
        if (_finishBlock) {
            _finishBlock();
        }
    });
    
    _isEndRequest = YES;
}

/**
 *  连接失败
 *
 *  @param connection <#connection description#>
 *  @param error      <#error description#>
 */
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    NSLog(@"出错信息:-- %@",error);
    _isEndRequest = YES;
}

@end

#import "NSOperationViewController.h"
#import "MyOperation.h"

@interface NSOperationViewController ()
{

    NSArray *_urls;
}
@property (nonatomic, strong)NSOperationQueue *queue;
@end

@implementation NSOperationViewController

#pragma mark - getter方法
- (NSOperationQueue *)queue
{
    if (!_queue) {
        _queue = [[NSOperationQueue alloc]init];
        _queue.maxConcurrentOperationCount = 3;
    }
    return _queue;
}



- (void)viewDidLoad {
    [super viewDidLoad];
   
    _urls = @[@"http://dl_dir.qq.com/qqfile/qq/QQforMac/QQ_V2.4.1.dmg",
              @"http://dl_dir.qq.com/qqfile/qq/QQforMac/QQ_V2.4.1.dmg",
              @"http://assets.sbnation.com/assets/2512203/dogflops.gif"];
    for (int i = 0; i < _urls.count; i++) {
        
        UIProgressView *progressView = (UIProgressView *)[self.view viewWithTag:i + 1];
        
        MyOperation *operation = [[MyOperation alloc]initWithUrl:[NSURL URLWithString:_urls[i]]];
        //设置进度回调
        [operation setOperationDIdFinishRecieveProgressBlock:^(float progress) {
            progressView.progress = progress;
        }];
        
        //设置下载完成回调
        [operation setOPerationDidFinish:^{
            NSString *message = [NSString stringWithFormat:@"第%d个任务完成",i+1];
           
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:message delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            [alert show];
        }];
        
        [self.queue addOperation:operation];
        
        
    }

//    BOOL a;
//    NSLog(@"%d",a);
    
    
    
#if 0
    
    添加依赖关系需要注意:
    1.添加依赖关系到 addOperation
    2.不要添加循环依赖

#endif
    
    //取消任务
    //[operation cancel];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void)awakeFromNib
{

}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
网络编程中,多线程编程是一种常用的技术,可以提高程序的并发性和性能。下面是一些关于多线程编程的常用方法和注意事项: 1. NSThread:NSThread是iOS中最底层的线程类,它可以通过类方法或实例方法来创建线程。使用NSThread可以设置线程的名称、优先级,以及控制线程的睡眠和退出等操作。 2. 线程调度:在多线程编程中,多个线程会并发运行,但线程的执行顺序是由CPU调度器决定的,程序员无法控制。多个线程会同时竞争CPU资源,谁先抢到资源谁就先执行,所以多线程的执行顺序是随机的。 3. 多线程的创建:在iOS开发中,常用的多线程编程方式有三种:NSThread、GCD和NSOperationNSThread是最底层的线程类,可以直接操作线程的各种属性和方法。GCD(Grand Central Dispatch)提供了一种高效的并发编程模型,可以通过队列来管理任务的执行。NSOperation是基于GCD的更高层次的封装,提供了更多的控制和管理线程的功能。 4. 线程的创建顺序:在多线程编程中,并不能保证哪个线程会先运行,即无法确定新创建的线程或调用线程哪个会先执行。新创建的线程可以访问进程的地址空间,并继承调用线程的浮点环境和信号屏蔽字,但挂起信号集会被清除。 总结来说,多线程编程是一种提高程序并发性和性能的技术,在网络编程中尤为重要。通过使用NSThread、GCD或NSOperation等方法,可以实现多线程的创建和管理。然而,程序员无法控制线程的执行顺序,因为线程的调度是由CPU调度器决定的。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [IOS之多线程基础(OC)](https://blog.csdn.net/yong_19930826/article/details/105857055)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [UNIX环境高级编程笔记](https://blog.csdn.net/w_x_myself/article/details/128613534)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值