returnData=[[myhttp.resultsDictionaryvalueForKey:@"body"]valueForKey:@"list"];
while (!returnData) {
returnData = [[myhttp.resultsDictionaryvalueForKey:@"body"]valueForKey:@"list"];
[[NSRunLoopcurrentRunLoop] runMode:NSDefaultRunLoopModebeforeDate:[NSDatedistantFuture]];
}
1.、NSThread
2、Cocoa NSOperation (iOS多線程編程之NSOperation和NSOperationQueue的使用)
3、GCD 全稱(chēng):Grand Central Dispatch( iOS多線程編程之Grand Central Dispatch(GCD)介紹和使用)
這三種編程方式從上到下,抽象度層次是從低到高的,抽象度越高的使用越簡(jiǎn)單,也是Apple最推薦使用的。
NSThread:
優(yōu)點(diǎn):NSThread 比其他兩個(gè)輕量級(jí)
缺點(diǎn):需要自己管理線程的生命周期,線程同步。線程同步對(duì)數(shù)據(jù)的加鎖會(huì)有一定的系統(tǒng)開(kāi)銷(xiāo)
//2.先創(chuàng)建線程對(duì)象,然后再運(yùn)行線程
NSThread* myThread = [[NSThread alloc] initWithTarget:self selector:@selector(doSomething:) object:nil];
[myThread start];
關(guān)于 waitUntilDone
- (void)changePopoverSize
{
[self performSelectorOnMainThread:@selector(changeText:) withObject:@"Happy aha111" waitUntilDone:YES];
NSLog(@"changePopoverSize#####end");
sleep(5);
NSLog(@"changePopoverSize-----end");
}
執(zhí)行結(jié)果如下:
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執(zhí)行完畢后再往下執(zhí)行
如果waitUntilDone:NO的話,結(jié)果如下:
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
聯(lián)系客服