iphone开发第五课:performSelectorOnMainThread 和detachNewThreadSelector区别

15 篇文章 0 订阅
  1. 举例说明怎么简单的创建一个子线程。  

  2.   

  3. 用到的类是NSThread类,这里使用detachNewTheadSelector:toTagaet:withObject创建一个线程。  

  4.   

  5. 函数setupThread:(NSArray*)userInfor。通过userInfor将需要的数据传到线程中。  

  6.   

  7. 函数定义:  

  8.   

  9. -(void)setupThread:(NSArray*)userInfor{  

  10.   

  11.    [NSThread detachNewThreadSelector:@selector(threadFunc:) toTarget:self withObject:(id)userInfor];  

  12.   

  13. }  

  14.   

  15. - (void)threadFunc:(id)userInfor{  

  16.   

  17.    NSAutoreleasePool*pool = [[NSAutoreleasePool alloc] init];  

  18.   

  19.    //。。。。需要做的处理。  

  20.   

  21.    //这里线程结束后立即返回  

  22.   

  23.   [self performSelectorOnMainThread:@selector(endThread) withObject:nil waitUntilDone:NO];  

  24.   

  25.   [pool release];  

  26.   

  27. }  

  28.   

  29. performSelectorOnMainThread通知主线程执行函数endThread。也可以使用performSelector:onThread:withObject:waitUntil 通知某线程执行线程结束后的处理。  

  30.   

  31. 线程内不要刷新界面。如果需要刷新界面,通过performSelectorOnMainThread,调出主线程中的方法去刷新。  

  32.   

  33.    

  34.   

  35. 例如,启动一个线程下载图片:  

  36.   

  37. //启动线程  

  38.   

  39. [NSThread detachNewThreadSelector:@selector(downloadImage:) toTarget:self withObject:url];  

  40.   

  41. //线程函数  

  42.   

  43. - (void) downloadImage:(NSString*)url{  

  44.       

  45.     _subThreed = [NSThread currentThread];  

  46.       

  47.     self.uploadPool = [[NSAutoreleasePool alloc] init];  

  48.     self.characterBuffer = [NSMutableData data];  

  49.     done = NO;  

  50.     [[NSURLCache sharedURLCache] removeAllCachedResponses];  

  51.       

  52.     NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:[NSURLURLWithString:url]];  

  53.       

  54.     self.connection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];  

  55.     [self performSelectorOnMainThread:@selector(httpConnectStart) withObject:nil waitUntilDone:NO];  

  56.     if (connection != nil) {  

  57.         do {  

  58.             [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];  

  59.         } while (!done);  

  60.     }  

  61.       

  62.     self.photo = [UIImage imageWithData:characterBuffer];  

  63.       

  64.   

  65.     //下载结束,刷新  

  66.     [self performSelectorOnMainThread:@selector(fillPhoto) withObject:nil waitUntilDone:NO];  

  67.       

  68.     // Release resources used only in this thread.  

  69.     self.connection = nil;  

  70.     [uploadPool release];  

  71.     self.uploadPool = nil;  

  72.       

  73.     _subThreed = nil;  

  74. }  

  75.   

  76.   

  77.   

  78. #pragma mark NSURLConnection Delegate methods  

  79.   

  80. /* 

  81.  Disable caching so that each time we run this app we are starting with a clean slate. You may not want to do this in your application. 

  82.  */  

  83. - (NSCachedURLResponse *)connection:(NSURLConnection *)connection willCacheResponse:(NSCachedURLResponse *)cachedResponse {  

  84.   

  85.     return nil;  

  86. }  

  87.   

  88. // Forward errors to the delegate.  

  89. - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {  

  90.     done = YES;  

  91.     [self performSelectorOnMainThread:@selector(httpConnectEnd) withObject:nil waitUntilDone:NO];  

  92.     [characterBuffer setLength:0];  

  93.       

  94. }  

  95.   

  96. // Called when a chunk of data has been downloaded.  

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

  98.     // Process the downloaded chunk of data.  

  99.    

  100.     [characterBuffer appendData:data];  

  101.       

  102. }  

  103.   

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

  105.       

  106.     [self performSelectorOnMainThread:@selector(httpConnectEnd) withObject:nil waitUntilDone:NO];  

  107.     // Set the condition which ends the run loop.  

  108.     done = YES;   

  109. }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值