自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

iPhone開(kāi)發(fā)應(yīng)用中NSOperation多線程使用

移動(dòng)開(kāi)發(fā) iOS
本文介紹介紹的iPhone開(kāi)發(fā)應(yīng)用中NSOperation多線程使用,詳細(xì)的介紹了NSOperation多線程的使用,先來(lái)看詳細(xì)內(nèi)容,

iPhone開(kāi)發(fā)應(yīng)用中NSOperation多線程使用是本文要介紹的內(nèi)容,首先創(chuàng)建一個(gè)線程類,RequestOperation,它繼承NSOperation,而后我們?cè)诳刂破黝惍?dāng)中,創(chuàng)建一個(gè)NSOperationQueue對(duì)象,將該線成加入到序列中。它就會(huì)自動(dòng)的從NSOperationQueue當(dāng)中取到我們加入的線程,而后運(yùn)行線成的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.  //第一個(gè)請(qǐng)求  
  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.  //第二個(gè)請(qǐng)求  
  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. //開(kāi)始處理  
  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. //取得數(shù)據(jù)  
  66. -(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{  
  67.  //添加數(shù)據(jù)  
  68.  [_data appendData:data];  
  69.  NSLog(@"%@",_data);  
  70. }  
  71. //http請(qǐng)求結(jié)束  
  72. -(void)connectionDidFinishLoading:(NSURLConnection *)connection{  
  73. }  
  74. @end 

小結(jié):iPhone開(kāi)發(fā)應(yīng)用中NSOperation多線程使用的內(nèi)容介紹完了,希望通過(guò)本文的學(xué)習(xí)對(duì)你有所幫助!

責(zé)任編輯:zhaolei 來(lái)源: 互聯(lián)網(wǎng)
相關(guān)推薦

2011-08-18 13:58:34

iPhone開(kāi)發(fā)NSOperation異步

2011-08-01 12:53:25

iPhone 多線程 線程

2011-08-10 10:18:22

iPhone多線程線程

2011-08-12 10:09:23

iPhone開(kāi)發(fā)多線程

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-08 14:07:49

iPhone開(kāi)發(fā) 字體

2011-08-17 15:10:21

iPhone開(kāi)發(fā)Web視圖

2011-08-10 17:37:00

iPhoneASIHTTPRequ

2011-06-07 17:35:39

iphone 多線程

2011-08-09 11:36:41

iPhoneUIPickerVieDEMO

2011-08-09 14:24:18

iPhone多線程線程

2011-08-09 17:12:30

iPhoneCFRunLoop

2011-08-12 14:33:06

iPhone緩存文件

2011-08-15 15:44:46

iPhone開(kāi)發(fā)PDF

2011-08-18 16:24:44

iPhone開(kāi)發(fā)圖片

2011-08-22 14:12:48

iPhone開(kāi)發(fā)NSTableView

2011-08-15 11:37:20

iPhone開(kāi)發(fā)Mask
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)