NSOperation使用

1.继承NSOperation

DownLoadImageTask.h
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@protocol DownLoadImageDelegate;//声明

@interface DownLoadImageTask : NSOperation<NSURLConnectionDelegate>{
    long long totalLength;
    BOOL done;
}

@property(nonatomic,weak) id<DownLoadImageDelegate>downloadImageDelegate;
@property(nonatomic,retain) NSMutableData *buffer;
@property(nonatomic,retain) NSURLRequest *request;
@property(nonatomic,retain) NSURLConnection *connection;

- (id)initWithURLString:(NSString *)url;

@end

@protocol DownLoadImageDelegate
//图片下载完成的委托
-(void)imageDownLoadFinished:(UIImage *)img;
//更新图片下载进度条的值
-(void)updateDownProgress:(double) value;
@end
#import "DownLoadImageTask.h"
@implementation DownLoadImageTask
- (id)initWithURLString:(NSString *)url{
    NSLog(@"url=%@",url);
    self = [super init];
    if(self){
        self.request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
        self.buffer = [NSMutableData data];
    }
    return self;
}

//主要处理方法
-(void)start{ //或者main
    NSLog(@"DownLoadImageTask-start");

    if(![self isCancelled]){
        //暂停一下
        [NSThread sleepForTimeInterval:1];
        //设置connection及其代理
        self.connection = [[NSURLConnection alloc]initWithRequest:self.request delegate:self];
        if(self.connection!=nil){
            while(!done){
                [[NSRunLoop currentRunLoop]runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
            }
        }
    }
}

-(void)httpConnectEndWithError{
    //[self hiddenWaiting];
    NSLog(@"httpConnectEndWithError");
}

-(void)dealloc{
    self.buffer = nil;
    self.connection = nil;
    self.request = nil;
    self.downloadImageDelegate = nil;
}

#pragma NSURLConnection delegate methods
//不执行缓存
-(NSCachedURLResponse *)connection:(NSURLConnection *)connection willCacheResponse:(NSCachedURLResponse *)cachedResponse{
    return nil;
}

//连接发生错误
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
    [self performSelectorOnMainThread:@selector(httpConnectEndWithError) withObject:self waitUntilDone:NO];
    [self.buffer setLength:0];
}

//收到响应
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
    if(httpResponse && [httpResponse respondsToSelector:@selector(allHeaderFields)]){
        NSDictionary *httpResponseHeaderFields = [httpResponse allHeaderFields];
        totalLength = [[httpResponseHeaderFields objectForKey:@"Content-Length"] longLongValue];
        NSLog(@"totalLength is %lld",totalLength);
    }
}

//接收数据
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
    //NSLog(@"didReceiveData...");
    [self.buffer appendData:data];

    double progressValue = totalLength==0?0:((double)([self.buffer length])/(double)totalLength);
    //更新进度条值
    //[(NSObject *)self.downloadImageDelegate performSelectorOnMainThread:@selector(updateDownProgress:) withObject:[NSNumber numberWithDouble:progressValue] waitUntilDone:NO];//主线程中执行代理方法
    [self.downloadImageDelegate updateDownProgress:progressValue];
}

//下载完毕
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
    done = YES;
    UIImage *img = [[UIImage alloc] initWithData:self.buffer];
    //[(NSObject *)self.downloadImageDelegate performSelectorOnMainThread:@selector(imageDownLoadFinished:) withObject:img waitUntilDone:NO];
    [self.downloadImageDelegate imageDownLoadFinished:img];
}

-(BOOL)isConcurrent {
    //返回yes表示支持异步调用,否则为支持同步调用
    return YES;

}

- (BOOL)isExecuting{
    return self.connection == nil;
}

- (BOOL)isFinished{
    return self.connection == nil;
}

@end

2.在ViewController中调用NSOperation子类

ViewController.h

#import <UIKit/UIKit.h>
#import "DownLoadImageTask.h"

@interface ViewController:UIViewController<DownLoadImageDelegate>

@property(strong,nonatomic)NSOperationQueue *queue;
@property(strong,nonatomic)UIImageView *appImgView;

@property (strong,nonatomic)UIImage *image1;
@property (strong,nonatomic)UIImage *image2;

@end

ViewController.m

#import "ViewController.h"
#import "DownLoadImageTask.h"

typedef void (^downLoad1)();
@interface ViewController()

@property (nonatomic,strong)downLoad1 downloadBlock;

@end

@implementation ViewController

#pragma mark - LoadView
-(void)loadView{
    //初始化视图
    [self initViews];

    //显示等待框
    [self showWaiting];
    NSString *url = @"http://hiphotos.baidu.com/newwen666666/pic/item/01ec7750863e49600cf3e3cc.jpg";
    DownLoadImageTask *task = [[DownLoadImageTask alloc]initWithURLString:url];
    task.downloadImageDelegate = self;
    //task.operationId = index++;

    self.queue = [[NSOperationQueue alloc]init];
    [self.queue addOperation:task];

}

//初始化视图组件
-(void)initViews{
    CGRect frame = [UIScreen mainScreen].applicationFrame;
    UIView *appView = [[UIView alloc]initWithFrame:frame];
    self.view = appView;
    [self.view setBackgroundColor:[UIColor colorWithWhite:1.0 alpha:1.0]];
    frame = CGRectMake(0, 0, frame.size.width, frame.size.height);
    _appImgView = [[UIImageView alloc]initWithFrame:frame];
    [self.view addSubview:_appImgView];
}

//展示等待框
-(void)showWaiting{
    CGRect frame = CGRectMake(0, -20, 320, 480);
    int x = frame.size.width;

    int progressWidth = 150;
    int progressHeight = 32;
    frame = CGRectMake((x-progressWidth)/2, 100, progressWidth, progressHeight);
    UIProgressView *progress = [[UIProgressView alloc]initWithProgressViewStyle:UIProgressViewStyleBar];
    progress.frame = frame;
    progress.progress = 0.0;
    progress.backgroundColor = [UIColor whiteColor];

    UILabel *showValue = [[UILabel alloc]init];
    frame = showValue.frame;
    frame.origin.x = CGRectGetMaxX(progress.frame)+10;
    frame.origin.y = CGRectGetMinY(progress.frame);
    frame.size.width = 80;
    frame.size.height = 15;
    showValue.frame = frame;
    showValue.backgroundColor = [UIColor redColor];
    showValue.text = @"0.0";

    int progressIndWidth = 32;
    int progressIndHeight = 32;
    frame = CGRectMake((x-progressIndWidth)/2, 100+32, progressIndWidth, progressIndHeight);
    UIActivityIndicatorView *progressInd = [[UIActivityIndicatorView alloc]initWithFrame:frame];
    [progressInd startAnimating];
    progressInd.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;

    frame = CGRectMake((x-70)/2, 100+32+32, 80, 20);
    UILabel *waitinglabel = [[UILabel alloc]initWithFrame:frame];
    waitinglabel.text = @"正在下载应用程序图片...";
    waitinglabel.textColor = [UIColor redColor];
    waitinglabel.font = [UIFont systemFontOfSize:15];
    waitinglabel.backgroundColor = [UIColor clearColor];

    frame = CGRectMake(0, 0, 320, 480);
    UIView *theView = [[UIView alloc]initWithFrame:frame];
    theView.backgroundColor = [UIColor blackColor];
    theView.alpha = 0.7;

    [progress setTag:100];
    [theView addSubview:progress];
    [showValue setTag:101];
    [theView addSubview:showValue];

    [theView addSubview:progressInd];
    [theView addSubview:waitinglabel];

    [theView setTag:110];
    [self.view addSubview:theView];
}

#pragma mark - ViewDidLoad

-(void)viewDidLoad{
    NSLog(@"%@",self.view.subviews);
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0, 200, 100);
    button.center = CGPointMake(self.view.center.x,self.view.center.y +100);
    [button addTarget:self action:@selector(downloadImage:) forControlEvents:UIControlEventTouchUpInside];
    [button setTitle:@"下载图片" forState:UIControlStateNormal];
    [self.view addSubview:button];
    __weak typeof(self) weakSelf = self;

    self.downloadBlock = ^{
        weakSelf.appImgView.image = weakSelf.image1;
        [NSTimer scheduledTimerWithTimeInterval:2.0 target:weakSelf selector:@selector(updateImage) userInfo:nil repeats:NO];
    };
}

- (void)updateImage{
    self.appImgView.image = self.image2;
}

#pragma mark - clickAction
- (void)downloadImage:(UIButton *)sender{
    [self downloadImages];
}

#pragma mark - DownLoadImages

- (void)downloadImages {
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

    // 异步下载图片
    dispatch_async(queue, ^{
        // 创建一个组
        dispatch_group_t group = dispatch_group_create();

        __block UIImage *image1 = nil;
        __block UIImage *image2 = nil;

        // 关联一个任务到group
        dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            // 下载第一张图片
            NSString *url1 = @"http://car0.autoimg.cn/upload/spec/9579/u_20120110174805627264.jpg";
            image1 = [self imageWithURLString:url1];
        });

        // 关联一个任务到group
        dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            // 下载第一张图片
            NSString *url2 = @"http://hiphotos.baidu.com/lvpics/pic/item/3a86813d1fa41768bba16746.jpg";
            image2 = [self imageWithURLString:url2];
        });

        // 等待组中的任务执行完毕,回到主线程执行block回调
        dispatch_group_notify(group, dispatch_get_main_queue(), ^{
            self.image1 = image1;
            self.image2 = image2;
            self.downloadBlock();
            // 千万不要在异步线程中自动释放UIImage,因为当异步线程结束,异步线程的自动释放池也会被销毁,那么UIImage也会被销毁
        });
    });
}

- (UIImage *)imageWithURLString:(NSString *)urlString {
    NSURL *url = [NSURL URLWithString:urlString];
    NSData *data = [NSData dataWithContentsOfURL:url];
    // 这里并没有自动释放UIImage对象
    return [[UIImage alloc] initWithData:data];
}

#pragma mark - DownLoadImageDelegate methods
//展示下载完毕的图片
-(void)imageDownLoadFinished:(UIImage *)img{
    //退出等待框
    [self hiddenWaiting];
    [self.appImgView setImage:img];
    NSLog(@"%@",self.view.subviews);

}

//更新进度条的值
-(void)updateDownProgress:(double) value{
    UIProgressView *progresss = (UIProgressView *)[self.view viewWithTag:100];
    UILabel *showValue = (UILabel*)[self.view viewWithTag:101];
    progresss.progress = value;
    showValue.text = [NSString stringWithFormat:@"%.1f%%",(double)(value*100)];
}

//隐藏等待框
-(void)hiddenWaiting{
    [[self.view viewWithTag:110]removeFromSuperview];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

@end
时间: 2024-10-10 05:38:15

NSOperation使用的相关文章

多线程之-NSOperation

多线程之-NSOperation NSOperation是个抽象类,并不具备封装操作的能力,必须使用它的子类 NSInvocationOperation 如果直接执行NSInvocationOperation中的操作, 那么默认会在主线程中执行 // 创建队列 NSOperationQueue *queue = [[NSOperationQueue alloc] init]; // 创建操作 NSInvocationOperation *operation = [[NSInvocationOpe

IOS中的多线程【二】— NSOperation和NSOperationQueue

NSOperationQueue是一套基于Objective-c语言的API. GCD与NSOperationQueue的优缺点: NSOperationQueue:比较安全 GCD:没有NSOperationQueue安全,但使用起来简单,快速,还提供了一些操控底层的方法.实际开发中还是以GCD为主. NSOperationQueue实现多线程流程 1.定义一个任务队列. 2.定义一个任务. 3.把任务添加到队列中.一旦任务被添加到队列中,任务会马上被调度执行. 任务队列(NSOperatio

iOS开发NSOperation 三:操作依赖和监听以及线程间通信

一:操作依赖和监听 #import "ViewController.h" @interface ViewController () @end @implementation ViewController /** * 1:NSOperation的使用:1:先创建队列NSOperationQueue:若不创建队列直接封装任务则默认在当前线程中串行执行任务,其队列分为两种主队列和非主队列,主队列和GCD中的主队列一样[NSOperationQueue mainQueue],而alloc in

线程2 NSOperation 抽像类的使用

// // ZYOperationViewController.h // Thread // // Created by yejiong on 14 // // ZYOperation.h // Thread // // Created by yejiong on 14/11/4. // Copyright © 2014年 zzz. All rights reserved. // #import <Foundation/Foundation.h> @interface ZYOperation

iOS中用GCD和NSOperation多个异步操作的关联

在iOS实际开发中,我们可能会遇到下面的场景:有以下四个操作A,B,C,D.要求A,B,C在子线程中执行,当A,B,C执行完毕之后回到主线程执行操作D,ABC之间可能会有相互依赖的关系,我们可以通过GCD和NSOperation都可以实现这样的需求. 1.GCD // 用GCD - (void)useGCD { // 1.1可以创建一个全局并发队列,A,B,C操作会在子线程中并发执行,ABC不存在先后执行顺序 // dispatch_queue_t quque=dispatch_get_glob

GCD和NSOperation 的概念,用法及之间的区别

CGD与NSOperation的区别 gcd是基于c的底层api,NSOperation属于object-c类.相对于gcd:1,NSOperation拥有更多的函数可用,具体查看api.2,在NSOperationQueue中,可以建立各个NSOperation之间的依赖关系.3,有kvo,可以监测operation是否正在执行(isExecuted).是否结束(isFinished),是否取消(isCanceld).4,NSOperationQueue可以方便的管理并发.NSOperatio

step 7 NSOperation(高级/GCD对比)

高级演练 全局队列 /// 全局操作队列,统一管理所有的异步操作 @property (nonatomic, strong) NSOperationQueue *queue; - (NSOperationQueue *)queue { if (_queue == nil) { _queue = [[NSOperationQueue alloc] init]; } return _queue; } 最大并发操作数 /// MARK: - 最大并发操作数 - (void)opDemo1 { // 设

iOS开发-NSOperation与GCD区别

Mac OS X 10.6及iOS4.0之后导入了可以使全体线程更高效运行,并且使并行处理应用更易开发的架构,GCD(Grand Central  Dispatch),同时引入的还有Run Loop,线程(包括Cocoa和POSIX)和Operation.GCD拥有非常轻量级的工作单元和并发方式,并且由系统决定其最佳调度方式.这个时候出现了一个问题,NSOperation如何处理呢? 其实我们在通过NSOperation和GCD进行开发过程中,会发现两者执行的方式有许多相似之处,NSOperat

多线程----NSOperation

NSOperation是Cocoa中的一个抽象类,用来封装单个任务和代码执行一项操作,由于是抽象类,所以不能直接实例化使用,必须定义子类继承该抽象类来实现, 使用NSOperation的方式有两种: 一种是用定义好的两个子类: NSInvocationOperation 和 NSBlockOperation,有相关的使用方法. NSOperation的子类NSInvocationOperation提供了一套简单的多线程编程方法,是IOS多线程编程中最简单的一种实现方式. 另一种是继承NSOper

NSOperation开启线程情况分析

如果NSOperation中只有一个任务,且调用start方法不会开启线程,start默认在当前线程执行. 如果NSOperation中只有一个任务,添加到队列当中会开启线程,如果是主队列则不会开启新的线程 一个NSOperation中添加了多个任务,且调用start方法,开启新的线程. 多个NSOperation,且每个NSOperation中只有一个任务,且每个NSOperation调用start方法 ,不会开启新的线程. 多个任务,添加到一个队列当中, 多个任务添加到主队列