networkActivityIndi​​catorVisible 不起作用(同步请求)

发布于 2024-12-24 03:29:56 字数 1187 浏览 0 评论 0原文

-(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;
}

我的代码有问题吗?或者同步请求有什么区别??? 有什么建议吗?或者我应该使用 AcivityIndi​​cator 视图吗?

-(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱你是孤单的心事 2024-12-31 03:29:56

我的猜测是,网络活动指示器需要某些视图的布局,也就是说,当您使其可见时,它会在某个时刻发送 [self setNeedsLayout] 。此方法不会立即导致布局,它只是将视图标记为需要布局,实际布局发生在运行循环结束时。您的情况的问题是您使用同步请求阻塞了主线程,以便在您再次使指示器不可见之后运行循环结束。

避免阻塞主线程的唯一方法是:从主线程的角度使其异步。您可以

  • 使用异步 NSURLConnection API;
  • 在后台线程上使用同步 NSURLConnection API;
  • 将同步 NSURLConnection API 与 NSOperationQueue 结合使用。

这个想法是显示网络活动指示器,启动网络请求,让主线程的运行循环旋转,并在请求完成时隐藏指示器。

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

  • use asynchronous NSURLConnection API;
  • use synchronous NSURLConnection API on a background thread;
  • use synchronous NSURLConnection API with NSOperationQueue.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文