dismissViewControllerAnimated:completion:
在應用中運行正常,就是不知道為什麼出現了幾秒鐘的延遲:
[api loginWithUsername:[dict objectForKey:@"username"] andPassword:[dict objectForKey:@"password"] andSuccessBlock:^(id json) {
NSLog(@"DONE... %@", [json objectForKey:@"status"]);
NSString *status = [json objectForKey:@"status"];
if([status isEqualToString:@"ok"]){
app.user = [json objectForKey:@"data"];
[self dismissViewControllerAnimated:YES completion:nil];
}else{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"could not log you in" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[alert show];
}
}];
在控制台可以看見DONE... ok。,意味著回調block執行正常,但是接下來需要三四秒鐘延遲視圖才能最終被dismiss。
怎麼解決這個延遲問題?謝謝
如果你不能保證UI代碼運行在主線程中,它可能就運行到其他地方去了,這樣的話會出現幾秒鐘的延遲。
添加下面的代碼可以讓dismissal運行在主線程中:
dispatch_async(dispatch_get_main_queue(), ^{
[self dismissViewControllerAnimated:YES completion:nil]
});