关于NSOperation的使用之一

栏目: IOS · 发布时间: 7年前

内容简介:Apple在OS X 10.5 Leopard上做了很多改进。以可以使用使用自己创建的

Apple在OS X 10.5 Leopard上做了很多改进。 NSThread 本身就新增了很多新的方法,从而使得多线程变得更加容易。此外还新增了 NSOperationNSOperationQueue 两个类,使多线程编程更加方便!

NSOperationNSOperationQueue 为例

1.头文件

//  QTileDownloadOperation.h
#import <Foundation/Foundation.h>

@interface QTileDownloadOperation : NSOperation

@property(nonatomic, retain)NSString* name;

- (void)clear;

@end

2.实现文件

//  QTileDownloadOperation.m
#import "QTileDownloadOperation.h"

@implementation QTileDownloadOperation
@synthesize name;

- (void)dealloc {
    [name release];

    //......
    [super dealloc];
}


- (void)main
{
    if ([self isCancelled] || [self isFinished]) {
        return;
    }

    NSLog(@"%@start!",name);
    NSString* urlString = [@"你得URL" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    NSURL* url = [NSURL URLWithString:urlString];
    NSMutableURLRequest* requestWithAgent = [NSMutableURLRequest requestWithURL:url];

    [requestWithAgent setValue:@"mapapi" forHTTPHeaderField:@"User-Agent"];
    [requestWithAgent setTimeoutInterval:120];

    NSError* error = nil;

    NSData *data = [NSURLConnection sendSynchronousRequest:requestWithAgent returningResponse:nil error:&error];

    if (error == nil) {

        if (![self isCancelled])
        {
            [self didFinishWithData:data];
        }
    }
    else
    {
       //error handle
       //对外通知,注意线程同步
    }
}

- (void)didFinishWithData:(NSData*)data
{
    NSLog(@"%@complete!",name);
    //数据处理.........................
    //对外通知,注意线程同步
}

- (void)clear
{
    //可以进行取消网络,delegate置为nil,清理其他资源等。主要是防止线程对象释放时Crash
}

@end

可以使用使用自己创建的 OperationQueue 管理线程对象

NSOperationQueue* q = [[NSOperationQueue alloc] init];
self.myQueue = q;
//设置允许最大并发数
[myQueue setMaxConcurrentOperationCount:2];
[q release];

NSOperationQueue 中添加 QTileDownloadOperation 对象

for (int i = 0; i < 8; ++i) {

    QTileDownloadOperation* op = [[QTileDownloadOperation alloc] init];
    NSString* name = [NSString stringWithFormat:@"op%d",i];
    op.name = name;
    [myQueue addOperation:op];
    [op release];
}

释放线程队列如下:

NSArray *allOperations = [myQueue operations];

for (QTileDownloadOperation* op in allOperations) {

    if(![op isCancelled])
    {
        [op clear];
        [op cancel];
    }
}

[myQueue release];
myQueue = nil;

那如何是取消呢?通常情况下,取消当前线程队列中当前待执行线程队列队首尚未取消的线程对象。如下所示:

NSArray *allOperations = [myQueue operations];
for (int i = [myQueue maxConcurrentOperationCount];  i < [allOperations count]; ++i) {
    QTileDownloadOperation* op = [allOperations objectAtIndex:i];

    if (!op.isCancelled) {

        [op clear];
        [op cancel];
    }
}

为什么会allOperations暂存呢?因为线程队列中的对象在并发执行,其状态在任意时间点可能会改变。而这个取消的操作通常在其他线程中,比如说主线程,会因线程不同步引发Crash等异常。


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

视觉链

视觉链

吴佳敏 / 机械工业出版社 / 59.00

这是一部能帮助视觉设计师开悟的著作,由携程网UED视觉高级经理撰写,是她9年互联网视觉设计经验的总结和奉献。 全书从设计师的专业能力、设计方向、设计技巧、设计理念、设计规范5个维度展开,其中前4项可以构成一个完整的视觉设计工作链,在这个链条上每一环都是后面一环的支撑,缺一不可。但是在这个链条之上必须配以设计规范,才能让这个链条更加稳固。因此本章主要分为5章: 第1章:首先介绍了互联网产......一起来看看 《视觉链》 这本书的介绍吧!

在线进制转换器
在线进制转换器

各进制数互转换器

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具