关于NSOperation的使用之一

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

内容简介: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等异常。


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

查看所有标签

猜你喜欢:

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

100个可操作的网络赚钱方法

100个可操作的网络赚钱方法

陶秋丰 / 云南科技 / 2009-12 / 29.80元

《100个可操作的网络赚钱方法》专为有志于网上创业的读者量身打造,作者是“实战型”的网赚高手,在17岁时就通过互联网创业“年人10万”,如今结合自身的亲身实战经验,与大家分享可以实实在在盈利的100个网络赚钱方法和技巧。内容包括:网站创建与推广、竞价广告、联盟赚钱、网站SEO优化、域名投资、广告投放盈利、威客、博客、淘客赚钱等多个方面。 本手册中作者结合自身的网络赚钱经历,通过具体的、可操作......一起来看看 《100个可操作的网络赚钱方法》 这本书的介绍吧!

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具

html转js在线工具
html转js在线工具

html转js在线工具

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试