networkActivityIndicatorVisible 不起作用(同步请求)
-(IBAction) webRequest;
{
response = [[NSMutableData alloc] init];
NSMutableURLRequest *theRequest = [[NSMutableURLRequest alloc]
initWithURL: [NSURL URLWithString:kResourcesURL]
cachePolicy: NSURLRequestReloadIgnoringLocalCacheData
timeoutInterval: 10];
[theRequest setValue:@"application/json" forHTTPHeaderField:@"accept"];
[theRequest setValue:strToken forHTTPHeaderField:@"token"];
//show network activity indicator
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
response = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&urlresponse error:&nserror];
NSString *strResponse = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
SBJsonParser *sbjasonparser = [[SBJsonParser alloc] init];
arrResponse = [sbjasonparser objectWithString:strResponse error:nil];
//hide network activity indicator
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}
我的代码有问题吗?或者同步请求有什么区别??? 有什么建议吗?或者我应该使用 AcivityIndicator 视图吗?
-(IBAction) webRequest;
{
response = [[NSMutableData alloc] init];
NSMutableURLRequest *theRequest = [[NSMutableURLRequest alloc]
initWithURL: [NSURL URLWithString:kResourcesURL]
cachePolicy: NSURLRequestReloadIgnoringLocalCacheData
timeoutInterval: 10];
[theRequest setValue:@"application/json" forHTTPHeaderField:@"accept"];
[theRequest setValue:strToken forHTTPHeaderField:@"token"];
//show network activity indicator
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
response = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&urlresponse error:&nserror];
NSString *strResponse = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
SBJsonParser *sbjasonparser = [[SBJsonParser alloc] init];
arrResponse = [sbjasonparser objectWithString:strResponse error:nil];
//hide network activity indicator
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}
Is there something wrong with my code? or Synchronized request makes any difference???
Any Suggestion ?? Or Should i use AcivityIndicator view?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我的猜测是,网络活动指示器需要某些视图的布局,也就是说,当您使其可见时,它会在某个时刻发送
[self setNeedsLayout]
。此方法不会立即导致布局,它只是将视图标记为需要布局,实际布局发生在运行循环结束时。您的情况的问题是您使用同步请求阻塞了主线程,以便在您再次使指示器不可见之后运行循环结束。避免阻塞主线程的唯一方法是:从主线程的角度使其异步。您可以
这个想法是显示网络活动指示器,启动网络请求,让主线程的运行循环旋转,并在请求完成时隐藏指示器。
My guess is that the network activity indicator requires a layout of some views, that is, when you make it (in)visible it sends
[self setNeedsLayout]
at some point. This method does not cause the layout immediately, it just marks the view as needing layout with the actual layout happening at the end of the run loop. The problem in your case is that you block the main thread with a synchronous request so that the end of the run loop occurs after you make the indicator invisible again.There is the only way to avoid blocking the main thread: make it asynchronous from the main thread's point of view. You can
The idea is to show the network activity indicator, start a network request, let the main thread's run loop spin, and hide the indicator when the request is complete.