关于等待
returnData=[[myhttp.resultsDictionary valueForKey:@"body"] valueForKey:@"list"];
while (!returnData) {
returnData = [[myhttp.resultsDictionary valueForKey:@"body"] valueForKey:@"list"];
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
}
iOS有三种多线程编程的技术,分别是:
1.、NSThread
2、Cocoa NSOperation (iOS多线程编程之NSOperation和NSOperationQueue的使用)
3、GCD 全称:Grand Central Dispatch( iOS多线程编程之Grand Central Dispatch(GCD)介绍和使用)
这三种编程方式从上到下,抽象度层次是从低到高的,抽象度越高的使用越简单,也是Apple最推荐使用的。
NSThread:
优点:NSThread 比其他两个轻量级
缺点:需要自己管理线程的生命周期,线程同步。线程同步对数据的加锁会有一定的系统开销
NSThread 有两种直接创建方式
//2.先创建线程对象,然后再运行线程
NSThread* myThread = [[NSThread alloc] initWithTarget:self selector:@selector(doSomething:) object:nil];
[myThread start];
- -(void)downloadImage:(NSString *) url{
- NSData *data = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:url]];
- UIImage *image = [[UIImage alloc]initWithData:data];
- if(image == nil){
- }else{
- //通知主线程
- [self performSelectorOnMainThread:@selector(updateUI:) withObject:image waitUntilDone:YES];
- }
- }
关于 waitUntilDone
- (void)changePopoverSize
{
[self performSelectorOnMainThread:@selector(changeText:) withObject:@"Happy aha111" waitUntilDone:YES];
NSLog(@"changePopoverSize#####end");
sleep(5);
NSLog(@"changePopoverSize-----end");
}
执行结果如下:
2012-08-17 17:19:29.618 awrbv[2024:f803] changeText:(NSString *)string
2012-08-17 17:19:29.619 awrbv[2024:f803] changePopoverSize#####end
2012-08-17 17:19:34.620 awrbv[2024:f803] changePopoverSize-----end
可以看出,如果waitUntilDone:YES那么等changeText执行完毕后再往下执行
如果waitUntilDone:NO的话,结果如下:
2012-08-17 17:21:12.135 awrbv[2049:f803] changePopoverSize#####end
2012-08-17 17:21:17.137 awrbv[2049:f803] changePopoverSize-----end
2012-08-17 17:21:17.139 awrbv[2049:f803] changeText:(NSString *)string