1

I need a way to perform a similar function like the one below, but instead of waiting 3 seconds, it waits for activity indicator to be hidden [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;. Any tips or suggestions are appreciated.

double delayInSeconds = 3.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds *    NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
   // lines of code
 });
rmaddy
  • 314,917
  • 42
  • 532
  • 579
user3606054
  • 591
  • 1
  • 5
  • 14

2 Answers2

2

How about you use Key-Value Observing:

Declare context

static int NetworkActivityContext;

Add your observer

[[UIApplication sharedApplication] addObserver:self forKeyPath:@"networkActivityIndicatorVisible" options:NSKeyValueObservingOptionNew context:&NetworkActivityContext];

Implement the KVO callback

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    if (context == &NetworkActivityContext) {
        BOOL isNetworkActivityVisible = [UIApplication sharedApplication].networkActivityIndicatorVisible;
        if(!isNetworkActivityVisible){
          //Do whatever work you need to do now that it's hidden
        }
    }
}
Tommy Devoy
  • 13,441
  • 3
  • 48
  • 75
  • I accepted this answer, but I am wondering if this is a good way to determine when to perform a task. I am setting some delegate to nil once this indicator stops so that way there aren't chances of delegates being called by my header request library. (If the delegates are called and the delegates is nil the App will crash). – user3606054 Sep 05 '14 at 20:33
  • I tried checking if delegates are nil before performing the header request, but sometimes it happens at the same time or when respondtoselector being called). Here is a stackoverflow question about it, but none of the solutions work. http://stackoverflow.com/questions/5701132/asihttprequest-crashes-my-app – user3606054 Sep 05 '14 at 20:38
  • I'm unfamiliar with whatever ASIHTTPRequest is.. KVO is definitely a great solution for being notified when the network activity indicator hidden status changes, however I cannot speak to the rest of the delegate code your talking about. I always just ensure I'm checking if(self.delegate && [self.delegate respondsToSelector:@"yourSelector"], and setting the delegate to nil when done with it. Would need more context, but kinda sounds like your dealing with a different issue. – Tommy Devoy Sep 05 '14 at 20:44
  • Also considering http://allseeing-i.com/ASIHTTPRequest/ says it stopped being updated in 2011, I'd say it's time to bail. – Tommy Devoy Sep 05 '14 at 20:46
  • I will change libraries ,but I have to update this App quickly. That will be my next priority. Also, can the network activity indicator be turned on by something other than your own App? – user3606054 Sep 05 '14 at 20:49
0

Do not rely on the activity indicator to determine the flow of the app. Where you set [UIApplication sharedApplication].networkActivityIndicatorVisible = NO you should also either fire a notification or otherwise indicate to a wider audience the activity has ended. Then listen for that notification and do what you need to do elsewhere.

If you are setting networkActivityIndicatorVisible inside the same class, or if you are performing a network call and can put what you need in the completion block or delegate callback, you should do that instead.

colinbrash
  • 481
  • 2
  • 11