NSOperation的使用细节 [2]

简介:

NSOperation的使用细节 [2]

 

 

这一节我们来写自定义nonconcurrent的operation,自定义nonconcurrent的operation很简单,重写main方法,之后处理好cancel事件即可。

 

在开始写nonconcurrent的operation之前,我们需要先了解几个关于NSOperationQueue的细节。

 

挂起操作

通常情况下,将操作添加到队列中是会立马执行的(如果没有设置队列的最大并发数目),将suspended设置成YES后会将没有执行的operation全部挂起。

 

NSOperationQueue无法重复添加NSOperation

并且,NSOperationQueue也无法移除已经添加的操作

 

NSOperationQueue的cancelAllOperations仅仅是将所有的NSOperation的isCancelled置位为YES而已(NSOperation自身的cancel操作也是将isCancelled设置成YES)

 

我们可以通过设置NSOperationQueue的maxConcurrentOperationCount来让操作按照添加顺序执行

注意哦,NSOperationQueue并不能将单个的NSOperation进行挂起操作,NSOperation自身也无法将自己暂停后再进行恢复操作,当NSOperation取消了之后,你再也无法对其进行恢复操作了,在NSOperationQueue上,你是无法实现的。

 

以下提供源码

//
//  NonconcurrentOperation.h
//  NSOperationExample
//
//  Created by YouXianMing on 15/9/4.
//  Copyright (c) 2015年 YouXianMing. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface NonconcurrentOperation : NSOperation

@property (nonatomic, strong)           NSString  *urlString;
@property (nonatomic, strong, readonly) NSData    *netData;

@end


//
//  NonconcurrentOperation.m
//  NSOperationExample
//
//  Created by YouXianMing on 15/9/4.
//  Copyright (c) 2015年 YouXianMing. All rights reserved.
//

#import "NonconcurrentOperation.h"

@interface NonconcurrentOperation ()

@property (nonatomic, strong) NSData   *netData;
@property (nonatomic)         BOOL      isDone;

@end

@implementation NonconcurrentOperation

- (void)main {
    
    if ([self isCancelled] == YES || [self isDone] == YES) {
        
        return;
    }
    
    NSURL *url            = [NSURL URLWithString:_urlString];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    
    self.netData = [NSURLConnection sendSynchronousRequest:request
                                         returningResponse:nil
                                                     error:nil];
    if (self.netData) {
        
        self.isDone = YES;
    }
    
    [self showThread];
}

- (void)showThread {

    if ([NSThread currentThread].isMainThread == YES) {
        
        NSLog(@"Run in MainThread %@", self.name);
        
    } else {
        
        NSLog(@"Run in SubThread  %@", self.name);
    }
}

@end


//
//  ViewController.m
//  NSOperationExample
//
//  Created by YouXianMing on 15/9/4.
//  Copyright (c) 2015年 YouXianMing. All rights reserved.
//

#import "ViewController.h"
#import "NonconcurrentOperation.h"

@interface ViewController () {

    NonconcurrentOperation *operation1;
    NonconcurrentOperation *operation2;
}

@property (nonatomic, strong) NSOperationQueue *queue;

@end

@implementation ViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    self.queue                             = [[NSOperationQueue alloc] init];
    self.queue.maxConcurrentOperationCount = 2;
    
    // 操作1
    operation1           = [[NonconcurrentOperation alloc] init];
    operation1.urlString = @"http://pic.cnblogs.com/avatar/607542/20150807105148.png";
    operation1.name      = @"operation1";
    
    // 操作2
    operation2           = [[NonconcurrentOperation alloc] init];
    operation2.urlString = @"http://pic.cnblogs.com/avatar/615197/20150505132152.png";
    operation2.name      = @"operation2";
    
    [self.queue addOperation:operation1];
    [self.queue addOperation:operation2];
    
    [self performSelector:@selector(event) withObject:nil afterDelay:1.f];
}

- (void)event {

    NSLog(@"%@", self.queue.operations);
}

@end

目录
相关文章
|
11天前
|
弹性计算 关系型数据库 微服务
基于 Docker 与 Kubernetes(K3s)的微服务:阿里云生产环境扩容实践
在微服务架构中,如何实现“稳定扩容”与“成本可控”是企业面临的核心挑战。本文结合 Python FastAPI 微服务实战,详解如何基于阿里云基础设施,利用 Docker 封装服务、K3s 实现容器编排,构建生产级微服务架构。内容涵盖容器构建、集群部署、自动扩缩容、可观测性等关键环节,适配阿里云资源特性与服务生态,助力企业打造低成本、高可靠、易扩展的微服务解决方案。
1230 5
|
10天前
|
机器学习/深度学习 人工智能 前端开发
通义DeepResearch全面开源!同步分享可落地的高阶Agent构建方法论
通义研究团队开源发布通义 DeepResearch —— 首个在性能上可与 OpenAI DeepResearch 相媲美、并在多项权威基准测试中取得领先表现的全开源 Web Agent。
1213 87
|
10天前
|
云栖大会
阿里云云栖大会2025年9月24日开启,免费申请大会门票,速度领取~
2025云栖大会将于9月24-26日举行,官网免费预约畅享票,审核后短信通知,持证件入场
1796 13
|
20天前
|
人工智能 运维 安全
|
3天前
|
资源调度
除了nrm-pm,还有哪些工具可以管理多个包管理器的源?
除了nrm-pm,还有哪些工具可以管理多个包管理器的源?
234 127
|
4天前
|
前端开发
Promise的then方法返回的新Promise对象有什么特点?
Promise的then方法返回的新Promise对象有什么特点?
177 2