Question

I am trying to load a UIActivityIndicatorView however have some confusion about then it should load.

Should it start on line [activityIndicatorView startAnimating]; or when it gets to the end of the function.

- (void)LoadBuayView{
    activityIndicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    activityIndicatorView.center = self.view.center;
    activityIndicatorView.backgroundColor = [UIColor grayColor];
    [activityIndicatorView hidesWhenStopped];
    [self.view addSubview:activityIndicatorView];
    [activityIndicatorView startAnimating];
}
- (IBAction) EditSave:(id)sender {

    [self LoadBuayView];

    for(int i =0; i<5; i++)
    {
        //Some very long takes time code
    }
}
Was it helpful?

Solution

the activityIndicatorView will start animation in the next runloop of mainThread, so you can think it get to start at end of the function.

You can try following code to show the indicator view:

- (IBAction) EditSave:(id)sender {
    [self LoadBuayView];
    [self performSelector:@selector(doLongTimeTask) withObject:nil afterDelay:0.01];

}

- (void)doLongTimeTask{
    for(int i =0; i<5; i++)
    {
        //Some very long takes time code
    }
}

OTHER TIPS

Any time consuming processes should not take place in the main queue, but rather you should employ asynchronous programming patterns as discussed in the Concurrency Programming Guide. Bottom line, you should never block the main queue. At best, blocking the main queue can result in a suboptimal UX, and at worst, your app can be killed by the iOS watch dog process.

Instead, dispatch time consuming code to a background queue (either a dispatch queue or an operation queue). You can either create your own custom background queue, or with GCD you can avail yourself of one of the existing background queues:

- (void)loadBuyView{
    activityIndicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    activityIndicatorView.center = self.view.center;
    activityIndicatorView.backgroundColor = [UIColor grayColor];
    [activityIndicatorView hidesWhenStopped];
    [self.view addSubview:activityIndicatorView];
    [activityIndicatorView startAnimating];
}

- (IBAction) didTouchUpInsideSaveButton:(id)sender {

    [self loadBuyView];

    // always do slow processes on a background queue, not the main queue

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        for (int i = 0; i < 5; i++)
        {
            // Some code that takes a very long time
        }

        // when done, dispatch the stopping of the activity indicator view back to the main queue;
        // all UI updates should be performed on the main queue

        dispatch_async(dispatch_get_main_queue(), ^{
            [activityIndicatorView stopAnimating];
        });
    });
}

For more information about asynchronous programming patterns like the above, see WWDC 2012 video Asynchronous Design Patterns with Blocks, GCD, and XPC. For background in the various concurrent programming technologies, see the Concurrency Programming Guide.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top