Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IOS - performSelector:withObject:afterDelay: NOT WORKING

Reference :

https://stackoverflow.com/a/14741253/1749293

Like the link above said , but it seems that it doesn't explain the reason.

In my code, the following will work :

dispatch_async(dispatch_get_main_queue(), ^{
       [self performSelector:  @selector(helloWorld) withObject:nil afterDelay:0.5];
}); 

but, when i comment something like this , (and I really sure that I run it in the main thread!!)the code doesn't work :

//    dispatch_async(dispatch_get_main_queue(), ^{
        [self performSelector:  @selector(helloWorld) withObject:nil afterDelay: 0.5];
//    });

Can somebody tell me why ? AND ' self ', will nerver release/deallocated , i retain it until the application is over.

" Not Working " , means that , (no crash) it doesn't jump into "helloWorld" method :

-(void) helloWorld {
    NSLog(@"hello world");     // I set a break point here for debug , it wouldn't pause forever
}

I think is the Run Loop cause this problem . Like this link said , but i need more details or more explicit explain.

like image 381
isaacselement Avatar asked Jun 06 '13 07:06

isaacselement


1 Answers

When I had this kind of thing happen, I was calling performSelector from a GCD dispatch. So it was setting the timer in the GCD worker thread which went away before the timer fired. When GCD removed the worker thread, the timer was lost, so the selector was never called.

like image 129
Walt Sellers Avatar answered Oct 20 '22 00:10

Walt Sellers