- 浏览: 79792 次
- 性别:
- 来自: 武汉
最新评论
-
woainike:
<div class="quote_title ...
performSelectorOnMainThread 和detachNewThreadSelector区别. -
hhb19900618:
兄弟 我没理解 _subThreed = [NSThread ...
performSelectorOnMainThread 和detachNewThreadSelector区别. -
woainike:
<div class="quote_title ...
performSelectorOnMainThread 和detachNewThreadSelector区别. -
hhb19900618:
你好 我想问一下: //下载结束,刷新 [self pe ...
performSelectorOnMainThread 和detachNewThreadSelector区别.
performSelectorOnMainThread 和detachNewThreadSelector区别.
- 博客分类:
- iPhone
举例说明怎么简单的创建一个子线程。
用到的类是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;
}
评论
Returns the thread object representing the current thread of execution.
+ (NSThread *)currentThread
Return Value
A thread object representing the current thread of execution.
这里面的片段代码,可不用关心。
[self performSelectorOnMainThread:@selector(fillPhoto) withObject:nil waitUntilDone:NO]; 它是怎么知道 下载结束了?难道performSelectorOnMainThread自动知道子线程运行完毕?
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
这是个delegate。
你实现这个方法后,当connection结束后就会被执行的。
[self performSelectorOnMainThread:@selector(fillPhoto) withObject:nil waitUntilDone:NO]; 它是怎么知道 下载结束了?难道performSelectorOnMainThread自动知道子线程运行完毕?
发表评论
-
个人网站建立了,后期数据移步www.rbbtsn0w.com
2013-09-04 23:00 1464个人网站建立了,后期数据移步www.rbbtsn0w.com -
UIRefreshControl for Tableview or TableViewController
2013-06-26 00:06 1267@interfaceViewController ( ... -
About search your address on MKMapView.
2013-06-09 22:47 1400When you have address it's a s ... -
针对UIBarButtonItem 下面的backbutton,一个很简单自定义方式。
2013-06-06 22:23 1272如果只需要用图片替换原来的back button,不需要里面 ... -
Custom your view and load in code. It's difference viewcontrolller.
2013-03-20 15:52 875You need to load it using t ... -
Xcode, git and .DS_Store
2012-11-28 21:23 1441On trying to merge two git b ... -
git for windows
2012-11-21 10:51 1197bulid 环境 scp 提交的命令 ... -
无聊的测试。release后还需要removeallobjects?
2012-11-14 16:42 1773#import <UIKit/UIKit.h> ... -
cookie观察
2012-10-24 12:51 839NSHTTPCookieStora ... -
iOS6对于shouldAutorotateToInterfaceOrientation的改动以及其他一些窗口相关细节
2012-10-11 11:40 3076OS6正式版发布当天博主我就更新了,随后也更新了对应的X ... -
Mac os 命令
2012-09-22 11:43 827打开终端输入命令:defaults writ ... -
DisMiss Keyboard,自定义的工具类
2012-09-17 17:15 1232// // Keyboard.h // Cust ... -
实效API
2012-08-02 12:04 1886iOS6 之后 presentModa ... -
APP 图片比例和名称规范 ,最新iPhone5比例
2012-07-25 21:10 2847从icon说起命名方法,举例 iPhone & ... -
The Icon and Default Images
2012-07-25 21:10 851The icon.png imag ... -
xcode的各个版本的下载地址
2012-03-29 10:51 2005现在进入apple的developer网站,默认只提供最 ... -
越狱开发
2012-03-23 18:07 0环境需要: Mac OS X 1 ... -
杂乱的草稿纸Cloud
2012-03-21 14:06 0NSAutoreleasePool *pool = [[ ... -
IP Address
2012-03-14 17:39 1010再来一篇,获取本地IP,三种方法: (1)在Day ... -
Copy Functions
2012-03-06 15:47 1271Copy Functions 在一般 ...
相关推荐
使用NSThread,我们可以创建一个新的线程来执行特定的任务,例如使用`[NSThread start]`、`[NSThread detachNewThreadSelector:toTarget:withObject:]`方法来启动一个新线程。通过NSThread,我们还可以在主线程上...
或者,如果使用了`detachNewThreadSelector`,则可以将UI更新操作作为目标方法的一部分,使用`self.performSelectorOnMainThread`。 现在,让我们讨论一下“一步下载”的概念。在某些情况下,可能需要一次性下载多...
此外,源码可能还涵盖了如何在主线程和后台线程之间切换,以更新UI(如使用`DispatchQueue.main.async`或`performSelectorOnMainThread`),因为所有的UI操作都必须在主线程上进行。 总之,"多线程_IOS应用源码.zip...
2. **NSThread**:可以直接创建线程,通过`detachNewThreadSelector:toTarget:withObject:`或`initWithTarget:selector:object:`方法,指定执行的selector、目标对象和参数。不过,线程的生命周期需要手动管理。 3....
1. **+ (void)detachNewThreadSelector:(SEL)aSelector toTarget:(id)aTarget withObject:(id)anArgument;** 这个类方法用于创建并启动一个新线程,执行指定的目标对象上的选择器方法。选择器、目标对象和可选参数是...
另一种是通过`detachNewThreadSelector:toTarget:withObject:`方法,指定执行的方法、目标对象以及参数。 `Block`,又称为闭包,是Objective-C的一个强大特性,它允许我们将一段代码封装起来,作为一个可传递的对象...
- 创建线程:可以通过NSThread的`detachNewThreadSelector:toTarget:withObject:`,GCD的`dispatch_async`,或者NSOperationQueue来实现。 - 主线程执行:`[someMethod performSelectorOnMainThread:@selector...
4. 线程同步:如果你需要在线程间进行通信,可以使用`NSThread`s的`detachNewThreadSelector:toTarget:withObject:`方法来启动新线程,或者使用NSLock、NSCondition等同步原语来避免竞态条件。 5. 资源管理:与其他...
//initWithNibName将控制器绑定xib的方法,如果xib的名称和控制器的类名称相同的时候,直接写init(会自动绑定同名的xib)就可以,如果xib的名称和类名称不同的话,必须手动调用此方法来进行绑定 ...
[NSThread detachNewThreadSelector:@selector(doSomething:) toTarget:self withObject:@"hi"]; ``` 此方法会在一个新的线程中执行`doSomething:`方法,并传入参数`"hi"`。 2. **实例方法**: ```objective-c ...
2. 在主线程上执行操作:`performSelectorOnMainThread:withObject:waitUntilDone:` 通常用于更新UI,确保在主线程执行。 3. 在当前线程执行操作:`performSelector:withObject:` 如果不需要切换线程,可以直接调用...