-1

I am trying to set a 'UIActivityIndicatorView' as an accessoryView in one of my 'UITableViewCell', in a such way as, the ActivityIndicator start animating when the user touch this cell. I add the following code at 'didSelectRowAtIndexPath' method:

UITableViewCell * cell = [tableView cellForRowAtIndexPath:indexPath];

UIActivityIndicatorView * activity = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];

NSThread * newThread1 = [[NSThread alloc]initWithTarget:self selector:@selector(carregarNoticias) object:nil];

switch (indexPath.row) {
    case 0:
        cell.accessoryView = activity;
        [activity startAnimating];
        sleep(0.01);
        [newThread1 start];

        while (![newThread1 isFinished]) {
            //waiting for thread
       }

        [activity stopAnimating];
[self.navigationController pushViewController:noticias animated:YES];
break; 

I was expecting 'UIActivityIndicatorView' animating while the newthread1 is running. However, the animating only start when the newThread1 finish (and therefore I don't want the animation anymore). I added 0.01 seconds as sleep time to give time to the animation start. Nevertheless, this also did't solve.

Does anyone know what my error is? I appreciate any help!

user3435595
  • 137
  • 1
  • 1
  • 9

1 Answers1

-1

I dont really know what you are trying to do adding delay like that.. try this line below, might be the one your look for.. and another thing.. please dont block the main thread like sleep(0.01); that, users wont like it..

cell.accessoryView = activity;

[activity startAnimating];

dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){

    // the system will wait until this 
    dispatch_async(dispatch_get_main_queue(), ^(void){

        [newThread1 start];

    });
    // is finished, maybe you dont need the `[NSThread sleepForTimeInterval:3];` 
    // pick the one that suits your implementation.. 

    [NSThread sleepForTimeInterval:3];

    dispatch_async(dispatch_get_main_queue(), ^(void){

        [activity stopAnimating];

        [self.navigationController pushViewController:noticias animated:YES];

    });
});

[NSThread sleepForTimeInterval:3] sleep the thread like the one you have sleep(0.01);

But using it inside

dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){  });

makes it async. and wont block the main thread..

while the dispatch_async(dispatch_get_main_queue(), ^(void){ fires your code at the main thread..

Happy coding, cheers! :)

0yeoj
  • 4,500
  • 3
  • 23
  • 41
  • Thanks for your replay! Could you please give me a little explanation about the code? It works partially: sometimes, the animation stops and the viewController is pushed before newThread1 finish, therefore, the content of the next viewController is not displayed correctly. Sometimes it works fine! What '[NSThread sleepForTimeInterval:3];' is actually doing? Thanks! – user3435595 Jun 06 '15 at 01:40
  • I've edited my answer, and added some more explanation, is it clearer now? also i rearrange the lines and added `[newThread1 start];` inside `dispatch_async(dispatch_get_main_queue(), ^(void){ });` to make use i run on the main thread. try it.. :) – 0yeoj Jun 06 '15 at 02:04
  • 1
    That's awesome! Works like a charm! It's clear now. Thank you! – user3435595 Jun 06 '15 at 02:27
  • I'll be glad if someone explain why my answer was downvoted.. for my edification.. – 0yeoj Jun 06 '15 at 13:36