iPhone開發應用中NSOperation多線程使用

來源:互聯網
上載者:User

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多線程使用的內容介紹完了,希望通過本文的學習對你有所協助!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.