问题描述
在我的应用程序中,当用户按下按钮时,我启动HTTP异步请求(使用 [NSURLConnection sendAsynchronousRequest ...]
)并更改<$ c $的文本c> UILabel 在 completionHandler
块中。但是,当请求结束时,这种变化不会发生,而是在2-3秒后发生。以下是导致此行为的代码段。
In my app when the user presses a button I start a HTTP asynchronous request (using [NSURLConnection sendAsynchronousRequest...]
) and change the text of UILabel
in the completionHandler
block. This change, however, does not take place when the request is concluded and instead happens around 2-3 seconds later. Below is a code snippet that results in this behavior.
- (IBAction)requestStuff:(id)sender
{
NSURL *url = [NSURL URLWithString:@"http://stackoverflow.com/"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSOperationQueue *queue = [[[NSOperationQueue alloc] init] autorelease];
[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:
^(NSURLResponse *response, NSData *data, NSError *error)
{
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
exampleLabel.text = [NSString stringWithFormat:@"%d", httpResponse.statusCode];
}];
}
当我尝试创建时会发生类似的行为
。 completionHandler
中的UIAlertView
A similar behavior happens when I attempt to create an UIAlertView
inside the completionHandler
.
- (IBAction)requestStuff:(id)sender
{
NSURL *url = [NSURL URLWithString:@"http://stackoverflow.com/"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSOperationQueue *queue = [[[NSOperationQueue alloc] init] autorelease];
[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:
^(NSURLResponse *response, NSData *data, NSError *error)
{
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
if ([httpResponse statusCode] == 200) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"It worked!"
message:nil
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
}
}];
}
然而,一个小小的区别是当<$ c $时屏幕变暗c> [alert show] 被执行。警报本身仅在2-3秒后出现,就像上一个场景一样。
A small difference, though, is that the screen dims when [alert show]
is executed. The alert itself only appears 2-3 seconds later like in the previous scenario.
我猜这与应用程序的线程如何处理UI有关,但是我不确定。任何关于延迟发生原因的指导将不胜感激。
I'm guessing this is related to how the UI is handled by the app's threads, but I'm not sure. Any guidance on why the delay happens will be greatly appreciated.
推荐答案
根据。
如果您的应用程序有图形用户界面,建议您从应用程序的主线程接收与用户相关的事件并启动界面更新。 此方法有助于避免与处理用户事件和绘制窗口内容相关的同步问题。某些框架(如Cocoa)通常需要这种行为,但即使对于那些没有这种行为的框架,将这种行为保留在主线程上也有利于简化管理用户界面的逻辑。
If your application has a graphical user interface, it is recommended that you receive user-related events and initiate interface updates from your application’s main thread. This approach helps avoid synchronization issues associated with handling user events and drawing window content. Some frameworks, such as Cocoa, generally require this behavior, but even for those that do not, keeping this behavior on the main thread has the advantage of simplifying the logic for managing your user interface.
在主线程上调用UI更新可以解决此问题。通过调用主线程(下面)来围绕UI代码。
Calling your UI updates on the main thread would solve this problem. Surround your UI code with a call to the main thread (below).
dispatch_async(dispatch_get_main_queue(), ^{
exampleLabel.text = [NSString stringWithFormat:@"%d", httpResponse.statusCode];
});
还有其他方法可以在主线程上进行调用,但是使用更简单的GCD命令可以执行工作。再次,请参阅了解更多信息。
There are other ways to do calls on the main thread, but using the simpler GCD commands would do the job. Again, see the Threaded Programming Guide for more info.
这篇关于更新异步HTTP请求的completionHandler中的视图时出现延迟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!