I have the following code which does a bit of housekeeping on a database:
-(void)housekeepDataBase{
NSLog(@"gets called");
UIView *translucentView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
translucentView.backgroundColor=[UIColor blackColor];
translucentView.alpha=0.65;
UIActivityIndicatorView *spinner=[[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
spinner.frame=CGRectMake(110, 221.5, 100, 37);
[translucentView addSubview:spinner];
[spinner startAnimating];
[self.view.window addSubview:translucentView];
dispatch_queue_t fetchQueue = dispatch_queue_create("fetch stuff", NULL);
dispatch_async(fetchQueue, ^{
[self deleteBlank];
[self housekeepHnmr];
[self housekeepCnmr];
dispatch_async(dispatch_get_main_queue(), ^{
[translucentView removeFromSuperview];
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"house"];
[[NSUserDefaults standardUserDefaults] synchronize];
NSLog(@"gets also called");
});
});
dispatch_release(fetchQueue);
}
Because it takes a while on the device to perform (25 seconds), I add translucent view and a spinner, so that the user knows he/she has to wait a bit.
The thing is that if this method is called with a taget action from a button, it works allright. However if I directly call the method from viewDidLoad, the database gets updated but the view doesn’t show up.
On the other hand if i implement the creation of the spinner inside fetchQueue, it works vice versa. Allright from viewDidLoad but not from the button/target action.
I’m probably doing it all wrong using a block that I don’t really need. Actually I don’t want the user to be able to touch anything during the update of the DB. But I found this snipped and, since it worked on the test I kept it.
Any suggestions on how to do it in a more proper way?
Thanks!
Instead of viewDidLoad, use viewDidAppear (but make sure that it only runs once). The view hasn’t been shown yet so you can’t add the spinner in viewDidLoad.