iPhone开发应用中NSOperation多线程使用

移动开发 iOS
本文介绍介绍的iPhone开发应用中NSOperation多线程使用,详细的介绍了NSOperation多线程的使用,先来看详细内容,

iPhone开发应用中NSOperation多线程使用是本文要介绍的内容,首先创建一个线程类,RequestOperation,它继承NSOperation,而后我们在控制器类当中,创建一个NSOperationQueue对象,将该线成加入到序列中。它就会自动的从NSOperationQueue当中取到我们加入的线程,而后运行线成的start方法。

  1. #import "RootViewController.h"  
  2. @implementation RootViewController  
  3. #pragma mark -  
  4. #pragma mark View lifecycle  
  5. -(void)buttonClicked:(id)sender{  
  6.  _queue=[[NSOperationQueue alloc] init];  
  7.  //第一个请求  
  8.  NSURLRequest *request=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http:www.google.com"]];  
  9.  RequestOperation *operation=[[RequestOperation alloc] initWithRequest:request];  
  10.  [_queue addOperation:operation];  
  11.  [operation release];  
  12.  //第二个请求  
  13.  //NSURLRequest *request2=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http:www.baidu.com"]];  
  14.  //RequestOperation *operation1=[[RequestOperation alloc]initWithRequest:request2];  
  15. //operation1.message=@"operation1---";  
  16. //[_queue addOperation:operation1];  
  17. }  
  18. #import <Foundation/Foundation.h> 
  19. @interface RequestOperation : NSOperation{  
  20.  NSURLRequest *_request;  
  21.  NSMutableData *_data;  
  22.  NSString *message;  
  23. }  
  24. @property(nonatomic,retain)NSString *message;  
  25. -(id)initWithRequest:(NSURLRequest*)request;  
  26. @end  
  27.    
  28. //  
  29. //  RequestOperation.m  
  30. //  NSOperation  
  31. //  
  32. //  Created by wangqiulei on 8/23/10.  
  33. //  Copyright 2010 __MyCompanyName__. All rights reserved.  
  34. //  
  35. #import "RequestOperation.h"  
  36. @implementation RequestOperation  
  37. @synthesize message;  
  38. -(id)initWithRequest:(NSURLRequest *)request{  
  39.    
  40.  if (self=[self init]) {  
  41.  _request=[request retain];  
  42.  _data=[[NSMutableData data]retain];  
  43.  }  
  44.  return self;  
  45. }  
  46. -(void)dealloc{  
  47.  [_request release];  
  48.  [_data release];  
  49.  [super dealloc];  
  50. }  
  51. //如果返回为YES表示asychronously方式处理  
  52. -(BOOL)isConcurrent{  
  53.    
  54.  return YES;  
  55. }  
  56. //开始处理  
  57. -(void)start{  
  58.  if (![self isCancelled]) {  
  59.    
  60.  NSLog(@"%@",self.message);  
  61.  NSLog(@"-------------%d",[self retainCount]);  
  62.  [NSURLConnection connectionWithRequest:_request delegate:self];  
  63.  }  
  64. }  
  65. //取得数据  
  66. -(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{  
  67.  //添加数据  
  68.  [_data appendData:data];  
  69.  NSLog(@"%@",_data);  
  70. }  
  71. //http请求结束  
  72. -(void)connectionDidFinishLoading:(NSURLConnection *)connection{  
  73. }  
  74. @end 

小结:iPhone开发应用中NSOperation多线程使用的内容介绍完了,希望通过本文的学习对你有所帮助!

责任编辑:zhaolei 来源: 互联网
相关推荐

2011-08-18 13:58:34

iPhone开发NSOperation异步

2011-08-01 12:53:25

iPhone 多线程 线程

2011-08-10 10:18:22

iPhone多线程线程

2011-08-12 10:09:23

iPhone开发多线程

2011-06-02 17:27:49

iphone 多线程

2011-07-21 11:12:58

iPhone 线程 多线程

2013-08-21 16:17:09

iPhone多线程

2011-07-08 16:43:46

iPhone Cocoa 多线程

2011-08-17 15:10:21

iPhone开发Web视图

2011-08-08 14:07:49

iPhone开发 字体

2011-06-07 17:35:39

iphone 多线程

2011-08-10 17:37:00

iPhoneASIHTTPRequ

2011-08-09 14:24:18

iPhone多线程线程

2011-07-21 15:20:31

iPhone SDK 多线程

2009-09-22 17:21:24

线程局部变量

2011-08-09 11:36:41

iPhoneUIPickerVieDEMO

2011-08-15 15:44:46

iPhone开发PDF

2011-08-09 17:12:30

iPhoneCFRunLoop

2011-08-12 14:33:06

iPhone缓存文件

2011-08-18 16:24:44

iPhone开发图片
点赞
收藏

51CTO技术栈公众号