如何在 sendAsynchronousRequest 的 completionHandler 中显示 UIAlertView?
how to display UIAlertView inside sendAsynchronousRequest's completionHandler?
我正在开发一个登录应用程序。我有一个注册视图,我正在使用 sendAsynchronousRequest 发送注册 request.When 我收到了返回的数据 我想显示显示注册(如果有效)的警报视图。现在我的问题是当我收到返回的数据时,UI 被阻止,直到显示警报视图。
为什么会这样,我该如何解决?
检查代码片段:
[NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
error = connectionError;
NSMutableDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil ];
if ([dic count] == 1) {
[dic valueForKey:@"RegisterUserResult"];
UIAlertView *alertview = [[UIAlertView alloc] initWithTitle:@"Registration" message:[dic valueForKey:@"RegisterUserResult"] delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:nil];
[alertview show];
}else {
UIAlertView *alertview = [[UIAlertView alloc] initWithTitle:@"Registration" message:[dic valueForKey:@"ErrorMessage"] delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:nil];
[alertview show];
}
[self printData:data];
您尝试在非主线程上执行 UI 操作,因为完成块在您的自定义操作队列上执行。
您需要将所有 UI 个调用包装在
中
dispatch_async(dispatch_get_main_queue(), ^{
......
});
在自定义操作队列中非主线程。
因此,在您的代码中,对 UIAlertView
的任何调用都将类似于
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertView *alertview = [[UIAlertView alloc] initWithTitle:@"Registration" message:[dic valueForKey:@"RegisterUserResult"] delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:nil];
[alertview show];
});
如果您的完成处理程序中没有繁重的操作,您可以在主线程上分派它,直接将队列设置为 [NSOperationQueue mainQueue]
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {...}];
我正在开发一个登录应用程序。我有一个注册视图,我正在使用 sendAsynchronousRequest 发送注册 request.When 我收到了返回的数据 我想显示显示注册(如果有效)的警报视图。现在我的问题是当我收到返回的数据时,UI 被阻止,直到显示警报视图。
为什么会这样,我该如何解决? 检查代码片段:
[NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
error = connectionError;
NSMutableDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil ];
if ([dic count] == 1) {
[dic valueForKey:@"RegisterUserResult"];
UIAlertView *alertview = [[UIAlertView alloc] initWithTitle:@"Registration" message:[dic valueForKey:@"RegisterUserResult"] delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:nil];
[alertview show];
}else {
UIAlertView *alertview = [[UIAlertView alloc] initWithTitle:@"Registration" message:[dic valueForKey:@"ErrorMessage"] delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:nil];
[alertview show];
}
[self printData:data];
您尝试在非主线程上执行 UI 操作,因为完成块在您的自定义操作队列上执行。
您需要将所有 UI 个调用包装在
中dispatch_async(dispatch_get_main_queue(), ^{
......
});
在自定义操作队列中非主线程。
因此,在您的代码中,对 UIAlertView
的任何调用都将类似于
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertView *alertview = [[UIAlertView alloc] initWithTitle:@"Registration" message:[dic valueForKey:@"RegisterUserResult"] delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:nil];
[alertview show];
});
如果您的完成处理程序中没有繁重的操作,您可以在主线程上分派它,直接将队列设置为 [NSOperationQueue mainQueue]
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {...}];