networkActivityIndicatorVisible

匿名 (未验证) 提交于 2019-12-03 01:58:03

问题:

Is this code correct to use with the networkActivityIndicatorVisible?

- (void)viewWillAppear:(BOOL)animated {      [super viewWillAppear:animated];      UIApplication* app2 = [UIApplication sharedApplication];      app2.networkActivityIndicatorVisible = YES;      [self loadSources];  // Loads data in table view     app2.networkActivityIndicatorVisible = NO;  } 

Teo

回答1:

Since NetworkActivityIndicatorVisible can be set from several points while a connection is still active, you need to track the number of calls that enable/disable it. The following UIApplication category does just that using a static variable:

// file UIApplication+NetworkActivity.h @interface UIApplication (NetworkActivity) - (void)showNetworkActivityIndicator; - (void)hideNetworkActivityIndicator; @end  // file UIApplication+NetworkActivity.m #import "UIApplication+NetworkActivity.h" static NSInteger activityCount = 0; @implementation UIApplication (NetworkActivity) - (void)showNetworkActivityIndicator {     if ([[UIApplication sharedApplication] isStatusBarHidden]) return;     @synchronized ([UIApplication sharedApplication]) {         if (activityCount == 0) {             [self setNetworkActivityIndicatorVisible:YES];         }         activityCount++;     } } - (void)hideNetworkActivityIndicator {     if ([[UIApplication sharedApplication] isStatusBarHidden]) return;     @synchronized ([UIApplication sharedApplication]) {         activityCount--;         if (activityCount 

Now import UIApplication+NetworkActivity.h in your client code and call

// on connection started: [[UIApplication sharedApplication] showNetworkActivityIndicator];  // on connection finished: [[UIApplication sharedApplication] hideNetworkActivityIndicator]; 

If your concern is that the indicator blinks for only a second, you don't need a background process. Just call [self performSelector:@selector(loadSources) withObject:Nil afterDelay:0.1] so the UI thread has time to start the network indicator animation before you block the main thread.



回答2:

An shorter solution for tracking multiple activities - again using a UIApplication category and a static variable:

@interface UIApplication (NetworkActivityIndicator)     - (void)toggleNetworkActivityIndicatorVisible:(BOOL)visible; @end  @implementation UIApplication (NetworkActivityIndicator) -(void)toggleNetworkActivityIndicatorVisible:(BOOL)visible {     static int activityCount = 0;     @synchronized (self) {         visible ? activityCount++ : activityCount--;         self.networkActivityIndicatorVisible = activityCount > 0;     } } @end 


回答3:

If you're not using AFNetworking (https://github.com/AFNetworking/AFNetworking) already you can check out their network activity indicator implementation in AFNetworkingActivityIndicatorManager.

If you do choose to use this library for your network access, they handle the network activity indicator for you automatically. All you need to do is make one call in your AppDelegate to set it up, they do the rest of the work for you.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {     [[AFNetworkActivityIndicatorManager sharedManager] setEnabled:YES]; } 


回答4:

I finally solved it. I used performSelectorInBackground to execute the load data into tableView

-(void)beginLoadSources {     [self loadSources];  // Loads data in table view     [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;  }  - (void)viewWillAppear:(BOOL)animated {     [super viewWillAppear:animated];     [self performSelectorInBackground:@selector(beginLoadSources) withObject:nil];     [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;  } 


回答5:

Use thins code in your ExempleUIWebView.m before - (void)viewDidLoad

  • (void)webViewDidFinishLoad:(UIWebView *)webView { [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; [ProgressHUD dismiss]; } and use this after - (void)viewDidLoad

[UIApplication sharedApplication].networkActivityIndicatorVisible = YES; [ProgressHUD show:@"Loading Privacy Policy" Interaction:NO];



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!