Since you use the function: selector in another thread, [NSRunLoop currentRunLoop] does not match the main thread.
See NSRunLoop Link :
If no sources or timers are connected to the start loop, this method ends immediately
I assume your launch cycle is empty, and so the BEFORE and AFTER logs will appear instantly.
A simple solution to your problem would be
@implementation myObject -(void)function:(id)param { NSLog(@"BEFORE"); [[NSRunLoop currentRunLoop] addTimer:[NSTimer timerWithTimeInterval:20 selector:... repeats:NO] forMode:NSDefaultRunLoopMode]; [[NSRunLoop currentRunLoop] run]; NSLog(@"AFTER"); } @end
In fact, you probably put the code that registers AFTER in the new method that your timer calls. In general, you do not need threads for animation (unless you are doing something expensive). If you are doing expensive things from a computational point of view, you should also consider using Grand Central Dispatch (GCD), which simplifies the calculations for loading background threads and will handle the plumbing for you.
source share