iphone开发-多线程
2011-06-02 22:39
267 查看
举例说明怎么简单的创建一个子线程。
用到的类是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:[NSURL
URLWithString: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;
}
用到的类是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:[NSURL
URLWithString: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;
}
相关文章推荐
- iphone开发使用NSThread和NSInvocationOperation实现多线程
- iphone 多线程开发
- iPhone开发【十四】多线程开发之NSThread——子线程模拟耗时操作
- [iphone开发多线程之]iOS的多核编程和内存管理
- [iphone开发多线程之]IPhone的多线程编程初体验
- iphone开发多线程介绍
- iPhone开发【十五】多线程开发之NSThread——异步下载图片
- IOS开发/iphone开发多线程
- iphone开发-多线程
- iPhone开发进阶(11)— 多线程的使用与注意事项
- iPhone软件开发之多线程初步
- iPhone开发多线程使用与注意事项
- iphone开发中的多线程:NSThread、NSOperation、GCD
- iPhone开发【十五】多线程开发之NSThread——异步下载图片
- iPhone开发之多线程使用
- iPhone开发进阶(11)--- 多线程的使用与注意事项
- iphone开发-多线程
- iPhone开发【十七】多线程开发之NSOperation&NSOperationQueue——异步下载图片
- [iphone开发多线程之]多线程之NSInvocationOperation
- [iphone开发多线程之]ios多线程开发总结