0
votes

is there a way to animate an UIActivityIndicator while constructing a complex UIView hierarchy? I have a complex hierarchy, to be added via 'addSubview:' in 'viewDidLoad' (in a mostly storyboard project, but this scene is dynamically created). Not fiddling with GCD, the UI blocks as long as initialization takes. Putting the initialization into 'dispatch_sync' (shown below) does not display/animate the indicator, putting it into 'dispatch_async (dispatch_get_global_queue (0,0),...' creates a crash (because UIKit code needs to run on main queue?), and putting it into 'dispatch_async (dispatch_get_main_queue(),...' creates a deadlock (as announces by Apple, I guess).

-(void)viewDidLoad {
    dispatch_sync (dispatch_get_global_queue (0,0), ^{
        // initialization here
        })
}

So what is the direction to go?

2

2 Answers

0
votes

Put the code in a method and try

– performSelectorOnMainThread:withObject:waitUntilDone:
– performSelectorOnMainThread:withObject:waitUntilDone:modes:
0
votes

Why don't you start the activity indicator in viewDidLoad, then schedule the initialization using performSelectorInBackground:

-(void)viewDidLoad {
    [super viewDidLoad];
    [self.activityIndicator startAnimating];
    [self performSelectorInBackground:@selector(finishInitializing) withObject:nil];
}

-(void)finishInitializing {
    // finish resource-intensive setup
    // ...

    dispatch_async(dispatch_get_main_queue(), ^{
        [self.activityIndicator removeFromSuperview];
        // create view hierarchy
        // ...
    });
}

This allows viewDidLoad to return so the spinner can animate on the main thread.