Reputation: 3854
in my app, I have a table in one of the viewControllers. When one of the rows are tapped, it takes the user to a different view/viewController and it works. Within this new viewController, data is being parsed from a php script in the background.
This takes about 7-10 seconds and in this time I want the user to IMMEDIATELY see a spinner that says "Loading..". I have implemented this myself but the spinner does not start to load until 4-5 seconds in. During this time, the screen is completely frozen and I cannot tap anything or go back until the spinner/data is displayed.
I have tried to put the following code within the method that does the actually fetching of the data, within both (not at the same time) the viewDidAppear and ViewDidLoad methods as well but the same thing happens.
If anyone knows how to fix this, it would be much appreciated.
Thank you
[web loadRequest: [NSURLRequest requestWithURL: [NSURL URLWithString:@"http://xxx.xx.xx.xxx/stuff/xxx.php"]]];
[web addSubview:spinner];
timer = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector: @selector(tick) userInfo:nil repeats:YES];
load_message = [[UIAlertView alloc] initWithTitle:@"Loading..." message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:nil];
spinner= [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
spinner.center = CGPointMake(135.0, 60.0);
[load_message addSubview:spinner];
-(void) tick
{
if(!web.loading)
{
[spinner stopAnimating];
[load_message dismissWithClickedButtonIndex:0 animated:TRUE];
}
else {
[load_message show];
[spinner startAnimating];
}
}
Upvotes: 0
Views: 950
Reputation: 9030
You could start your spinner then start your long process after a delay:
- (void)someMethod
{
[spinner startAnimating];
[self performSelector@selector(doLongProcess:) withObject:someObject afterDelay:0.0];
}
- (void)doLongProcess:(id)someObject
{
//Some really long process
}
Your spinner was most likely blocked because a long process may be occurring on the same thread.
Upvotes: 2
Reputation: 6678
You have to do one of the following (assumes you put your web request in a method called 'startWebRequest'):
Start the UIActivityIndicatorView on the the main UI thread (if not already running from the main thread):
[self performSelectorOnMainThread:@selector(startWebRequest) withObject:nil waitUntilDone:NO];
Start the web request on a background thread:
[self performSelectorInBackground:@selector(startWebRequest) withObject:nil];
Pause like a 1/10th of second before starting the web request like with:
[NSTimer scheduledTimerWithTimeInterval:0.1f target:self selector:@selector(startWebRequest) userInfo:nil repeats:NO];
Upvotes: 1