0
在UIViewController
viewDidAppear
事件中,我想從Web服務獲取一些數據。而這樣的代碼:「[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]]」使UI無響應
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
NSArray *arr = [self getCarList];
}
- (NSArray *)getCarList
{
if (!carList) {
ARequset *req = [[ARequset alloc] init];
[NetService sendRequest:req respClass:[Resp class] success:^(BaseResponse *response)
{
//after finished
self.requestFinished = YES;
} fail:^(NSInteger errcode, NSString *errmsg) {
self.requestFinished = YES;
}];
while (!self.requestFinished) {
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
}
}
return carList;
}
時至運行請求成功塊將不被執行,並且UI變得沒有反應。
但如果我改變這樣的- (void)viewDidAppear:(BOOL)animated
,一切順利。
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self performSelector:@selector(getCarList) withObject:self afterDelay:1];
}
但是,getCarList方法位於單例類中。我想用一個單例來保存cardList。是卡列表不是零,直接返回,否則請求從服務器返回。 @vadian – bohan
我編輯帖子向你展示另一種方式 – vadian
呵呵你一臉(非常感謝)@vadian – bohan