调用网络服务时显示警报 [英] Showing alert while calling webservice

查看:66
本文介绍了调用网络服务时显示警报的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码。

UIActivityIndicator *activity = [[UIActivityIndicator alloc] initWithActivityIndicatorStyle:
                                                    UIActivityIndicatorStyleWhite];


UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Processing" delegate:self otherButtonTitles:nil];
   [alert addSubview:activity];    
   [activity startAnimating];    
   [alert show];

WebServiceController *web = [[WebServiceController alloc]init];

NSDictionary *dict = [web getDetails];

问题是未显示警报。 WebServiceController是一个XML解析器,它从指定的URL获取详细信息并返回它们。调用服务时应显示警报,因为获取详细信息需要时间。但是仅在服务呼叫结束后才显示警报。为什么会这样?

The problem is that the alert is not getting displayed. The WebServiceController is an XML parser which gets details from a specified URL and returns them. The alert should be shown while the service is called as it takes time to get the details. But it displays the alert only after the service call is over. Why is that?

推荐答案

因为[alert show]将需要动画,因为服务控制器调用发生在主线程上,主线程正忙于执行服务调用,从而阻止执行警报视图动画。

because [alert show] will require animation, since the service controller call is taking place on the main thread, the main thread is busy executing the service call, blocking the alert view animation to execute.

您需要在后端线程上执行ServiceCall,请参见NSOperation或PerformSelectorOnBackgroundThread,确保您将具有AlertView的ViewController的委托传递给后端线程,在服务调用完成后立即回调该委托。确保使用performSelectorOnMainThread在主线程上执行对回调的调用。与UI相关的所有调用都应在主线程上执行。

You need to perform the ServiceCall on backend thread, see NSOperation or PerformSelectorOnBackgroundThread, make sure you pass in delegate of the ViewController that has the AlertView to the backend thread, callback the delegate as soon as the service call is completed. Make sure you perform the call for the callback on the mainthread using performSelectorOnMainThread. All UI related calls should be executed on the main thread.

这篇关于调用网络服务时显示警报的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆