1

I have two view controllers; in the second view, a bunch of data are processed which takes pretty much time, while in the first view, there is a button navigating to the second. I want to display an activity indicator for the process in the second view right after the button clicked. But initialising UIActivityIndicatorView in the second view doesn't seem to work. Nothing showed up when the button was clicked, and the app was stuck in the first view when data being processed.

Below are the code I wrote in viewDidLoad in the second view controller.

_activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
[_activityIndicator setCenter:CGPointMake(SCREEN_WIDTH/2, SCREEN_HEIGHT/2)];
[self.view addSubview:_activityIndicator];

...............

[_activityIndicator startAnimating];

...............
// data processing

[_activityIndicator stopAnimating];

Anyone know how to solve this?

========EDIT=========

Thank you so much for the advices. Now I've tried using NSThread,but the spinner showed up pretty late. Here are the code I wrote in the first view controller.

- (void)viewDidLoad
{
    [super viewDidLoad];

    // activity indicator
    _activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    [_activityIndicator setCenter:CGPointMake(SCREEN_WIDTH/4, SCREEN_HEIGHT/4)];
    [self.view addSubview:_activityIndicator];
}

- (IBAction)startButtonClicked:(id)sender
{
    [NSThread detachNewThreadSelector:@selector(threadStartAnimating:) toTarget:self withObject:nil];
}

-(void)threadStartAnimating:(id)data
{
    NSLog(@"start");
    [_activityIndicator startAnimating];
    [self performSelectorOnMainThread:@selector(threadStopAnimating:) withObject:nil waitUntilDone:YES];
}


-(void)threadStopAnimating:(id)data
{
    NSLog(@"stop");
    [_activityIndicator stopAnimating];
}

The spinner appeared around 2 sec after NSLog(@"start"); being executed and showed up in a very short period. I linked - (IBAction)startButtonClicked:(id)sender with the button that navigated to the second view. Is there any better way to put [_activityIndicator startAnimating];?

5
  • Start the new Thread in secondview for uiactivityindicator to start load, because i think in mainthread your data is loading. Use [NSThread detachNewThreadSelector:@selector(showIndicator) toTarget:self withObject:nil]; Or use Dispatch_async method. Commented Mar 9, 2015 at 7:11
  • your code in viewDidLoad looks ok... maybe the issue is in the first view, can you show some code there? Commented Mar 9, 2015 at 7:11
  • "Nothing showed up when the button was clicked, and the app was stuck in the first view when data being processed." what exactly do you mean with "was stuck"? Commented Mar 9, 2015 at 7:12
  • use this.. ranga-iphone-developer.blogspot.in/2010/07/…. Commented Mar 9, 2015 at 7:16
  • @nburk I hadn't written any code in the first view, but now I've tried to use NSThread. I wrote some code in the edit part. I mean after clicking the button, it stayed in the first view for a moment while the data were being loaded in the second one. Sorry for my English. Commented Mar 9, 2015 at 13:13

1 Answer 1

2

Maybe you can try to solve this issue with Grand Central Dispatch (threads).

In you second VC, try this code:

- (void) viewDidLoad{
[super viewDidLoad];

// Main thread
_activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
[_activityIndicator setCenter:CGPointMake(SCREEN_WIDTH/4, SCREEN_HEIGHT/4)];
[self.view addSubview:_activityIndicator];
[_activityIndicator startAnimating];

// create a queue
dispatch_queue_t queue = dispatch_queue_create("data_process", 0);

// send a block to the queue - Not in Main thread
dispatch_async(queue, ^{
// data processing
.................

    // Interaction with User Interface - Main thread
    dispatch_async(dispatch_get_main_queue(), ^{
        [_activityIndicator stopAnimating];
        _activityIndicator.hidden = YES;
    });
});
}

I hope this helps.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.