舉例說明怎麼簡單的創建一個子線程


用到的類是NSThread類,這裏使用detachNewTheadSelector:toTagaet:withObject創建一個線程。

函數setupThread:(NSArray*)userInfor。通過userInfor將需要的數據傳到線程中。

函數定義:

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

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

}

- (void)threadFunc:(id)userInfor{

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

   //。。。。需要做的處理。

   //這裏線程結束後立即返回

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

  [pool release];

}

performSelectorOnMainThread通知主線程執行函數endThread。也可以使用performSelector:onThread:withObject:waitUntil 通知某線程執行線程結束後的處理。

線程內不要刷新界面。如果需要刷新界面,通過performSelectorOnMainThread,調出主線程中的方法去刷新。

 

例如,啓動一個線程下載圖片:

//啓動線程

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

//線程函數

- (void) downloadImage:(NSString*)url{
    
    _subThreed = [NSThread currentThread];
    
    self.uploadPool = [[NSAutoreleasePool alloc] init];
    self.characterBuffer = [NSMutableData data];
    done = NO;
    [[NSURLCache sharedURLCache] removeAllCachedResponses];
    
    NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:[NSURLURLWithString:url]];
    
    self.connection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
    [self performSelectorOnMainThread:@selector(httpConnectStart) withObject:nil waitUntilDone:NO];
    if (connection != nil) {
        do {
            [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
        } while (!done);
    }
    
    self.photo = [UIImage imageWithData:characterBuffer];
    

    //下載結束,刷新
    [self performSelectorOnMainThread:@selector(fillPhoto) withObject:nil waitUntilDone:NO];
    
    // Release resources used only in this thread.
    self.connection = nil;
    [uploadPool release];
    self.uploadPool = nil;
    
    _subThreed = nil;
}


#pragma mark NSURLConnection Delegate methods

/*
 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.
 */

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

    return nil;
}

// Forward errors to the delegate.
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    done = YES;
    [self performSelectorOnMainThread:@selector(httpConnectEnd) withObject:nil waitUntilDone:NO];
    [characterBuffer setLength:0];
    
}

// Called when a chunk of data has been downloaded.
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    // Process the downloaded chunk of data.
 
    [characterBuffer appendData:data];
    
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    
    [self performSelectorOnMainThread:@selector(httpConnectEnd) withObject:nil waitUntilDone:NO];
    // Set the condition which ends the run loop.
    done = YES; 
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章