IOS学习 performSelectorOnMainThread 和detachNewThreadSelector的使用

来源:互联网 发布:java获取ip 端口号 编辑:程序博客网 时间:2024/05/16 07:04

  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
原创粉丝点击