iphone NSOperation多线程使用

#import <Foundation/Foundation.h>

 

 

@interface RequestOperation : NSOperation{

 NSURLRequest *_request;

 NSMutableData *_data;

 NSString *message;

 

 

}

@property(nonatomic,retain)NSString *message;

-(id)initWithRequest:(NSURLRequest*)request;

 

@end

 

 

 

//

//  RequestOperation.m

//  NSOperation

//

//  Created by wangqiulei on 8/23/10.

//  Copyright 2010 __MyCompanyName__. All rights reserved.

//

 

#import "RequestOperation.h"

 

 

@implementation RequestOperation

@synthesize message;

-(id)initWithRequest:(NSURLRequest *)request{

 

 if (self=[self init]) {

 _request=[request retain];

 _data=[[NSMutableData data]retain];

 }

 

 return self;

}

 

-(void)dealloc{

 [_request release];

 [_data release];

 [super dealloc];

 

}

//如果返回为YES表示asychronously方式处理

-(BOOL)isConcurrent{

 

 return YES;

 

}

 

//开始处理

-(void)start{

 if (![self isCancelled]) {

 

 NSLog(@"%@",self.message);

 NSLog(@"-------------%d",[self retainCount]);

 [NSURLConnection connectionWithRequest:_request delegate:self];

 }

 

}

 

//取得数据

-(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{

 //添加数据

 

 [_data appendData:data];

 

 NSLog(@"%@",_data);

 

 

}

 

//http请求结束

 

-(void)connectionDidFinishLoading:(NSURLConnection *)connection{

 

 

 

}

@end

 

 

 

 

 

 

 

 

 

 

#import "RootViewController.h"

 

 

@implementation RootViewController

 

 

#pragma mark -

#pragma mark View lifecycle

 

/*

- (void)viewDidLoad {

    [super viewDidLoad];

 

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.

    // self.navigationItem.rightBarButtonItem = self.editButtonItem;

}

*/

 

/*

- (void)viewWillAppear:(BOOL)animated {

    [super viewWillAppear:animated];

}

*/

/*

- (void)viewDidAppear:(BOOL)animated {

    [super viewDidAppear:animated];

}

*/

/*

- (void)viewWillDisappear:(BOOL)animated {

 [super viewWillDisappear:animated];

}

*/

/*

- (void)viewDidDisappear:(BOOL)animated {

 [super viewDidDisappear:animated];

}

*/

 

/*

 // Override to allow orientations other than the default portrait orientation.

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {

 // Return YES for supported orientations.

 return (interfaceOrientation == UIInterfaceOrientationPortrait);

}

 */

 

-(void)buttonClicked:(id)sender{

 _queue=[[NSOperationQueue alloc] init];

 

 //第一个请求

 NSURLRequest *request=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http:www.google.com"]];

 RequestOperation *operation=[[RequestOperation alloc] initWithRequest:request];

 

 

 [_queue addOperation:operation];

 [operation release];

 

 

 //第二个请求

 //NSURLRequest *request2=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http:www.baidu.com"]];

 //RequestOperation *operation1=[[RequestOperation alloc]initWithRequest:request2];

//operation1.message=@"operation1---";

 

//[_queue addOperation:operation1];

}

posted @ 2010-11-16 10:43  SsQq  阅读(551)  评论(0编辑  收藏  举报