IOS学习 performSelectorOnMainThread 和detachNewThreadSelector的使用
来源:互联网 发布:java获取ip 端口号 编辑:程序博客网 时间:2024/05/16 07:04
- 举例说明怎么简单的创建一个子线程。
- 用到的类是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;
- }
0 0
- IOS学习 performSelectorOnMainThread 和detachNewThreadSelector的使用
- performSelectorOnMainThread 和detachNewThreadSelector区别
- performSelectorOnMainThread 和detachNewThreadSelector区别.
- performSelectorOnMainThread 和detachNewThreadSelector区别
- performSelectorOnMainThread 和detachNewThreadSelector区别.
- performSelectorOnMainThread 和detachNewThreadSelector区别.
- performSelectorOnMainThread 和detachNewThreadSelector区别.
- performSelectorOnMainThread 和detachNewThreadSelector区别
- IOS线程的使用 performSelectorOnMainThread
- iphone开发第五课:performSelectorOnMainThread 和detachNewThreadSelector区别
- IOS学习--performSelectorOnMainThread
- IOS学习--performSelectorOnMainThread
- iOS: 学习笔记, performSelectorOnMainThread
- iOS: 学习笔记, 使用performSelectorOnMainThread及时刷新UIImageView
- iphone 线程总结— detachNewThreadSelector的使用
- iphone 线程总结— detachNewThreadSelector的使用
- iphone 线程总结— detachNewThreadSelector的使用
- iphone 线程总结— detachNewThreadSelector的使用
- PAT B1020或者A1070 月饼注意设置看清数据类型
- 1044. Shopping in Mars
- 理想低通滤波器、巴特沃斯低通滤波器和高斯低通滤波器
- Linux设备模型——设备驱动模型和sysfs文件系统解读
- FFmpeg源代码简单分析:avformat_find_stream_info()
- IOS学习 performSelectorOnMainThread 和detachNewThreadSelector的使用
- IE8 下的文件上传问题
- 2014年最受欢迎的6个前端开发框架
- Go语言操作Redis
- 关于Spring中ApplicationContext的说明
- 面向对象语言Objective-C三大特性之三--多态
- 我的前端架构养成记之整洁的工作流
- css编写三角形汇总
- hadoop no xxxx to stop的原因和解决方法