-(void)buttonClick:(id)sender
{
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
for(int i=0;i<500000000;i++)
{
//Simulates network activity
}
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}
Theoretically, the network activity spinner in the top left corner of the screen should turn on, spin for a while, then turn off…however, it never shows up.
-(void)buttonClick:(id)sender
{
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
for(int i=0;i<500000000;i++)
{
//Simulates network activity
}
//[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}
With “= NO” being commented out, I noticed that the program runs through the entire for loop, then after completion of the for loop, the spinner finally starts spinning.
-(void)viewWillAppear:(BOOL)animated
{
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES
}
-(void)buttonClick:(id)sender
{
for(int i=0;i<500000000;i++)
{
//Simulates network activity
}
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}
Now, when the view appears, the indicator starts spinning. When I click the button, it continues spinning while the loop is running. When the for loop ends, the spinner stops as expected.
-=-=-=-=-=-=-=-=-=-
My question is this, why won’t the spinner start spinning till after the for loop in the first 2 examples?
-=-=-=-=-=-=-=-=-=-
My thinking thus far:
In terms of why the spinner won’t start in this specific example…Perhaps it sends the message, but doesn’t get executed till after the for loop, because of how the compiled code works?
Perhaps the correct way of doing this is to enable the network activity indicator, then shoot off another thread that does the network activity, then that thread shoots a message back to the main thread when it’s done (presumably with the data it retrieved as well)
-=-=-=-=-=-=-=-=-
Any advice/answers are appreciated. Thanks in advance. Keep in mind this app is for iPad.
The UI will not update until the end of the current run loop. Your method currently blocks the main thread whilst doing it’s work, which does not give your program a chance to reach the end of the runloop.
So what you need to do is take your long running task off of the main thread