nsrunloop
Using NSURLSession from a Swift command line program
You can use a semaphore to block the current thread and wait for your URL session to finish. Create the semaphore, kick off your URL session, then wait on the semaphore. From your URL session completion callback, signal the semaphore. You could use a global flag (declare a volatile boolean variable) and poll that from … Read more
How do you schedule a block to run on the next run loop iteration?
You might not be aware of everything that the run loop does in each iteration. (I wasn’t before I researched this answer!) As it happens, CFRunLoop is part of the open-source CoreFoundation package, so we can take a look at exactly what it entails. The run loop looks roughly like this: while (true) { Call … Read more
RunLoop vs DispatchQueue as Scheduler
There actually is a big difference between using RunLoop.main as a Scheduler and using DispatchQueue.main as a Scheduler: RunLoop.main runs callbacks only when the main run loop is running in the .default mode, which is not the mode used when tracking touch and mouse events. If you use RunLoop.main as a Scheduler, your events will … Read more
How do I create a NSTimer on a background thread?
If you need this so timers still run when you scroll your views (or maps), you need to schedule them on different run loop mode. Replace your current timer: [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(timerFired:) userInfo:nil repeats:YES]; With this one: NSTimer *timer = [NSTimer timerWithTimeInterval:0.5 target:self selector:@selector(timerFired:) userInfo:nil repeats:YES]; [[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes]; For details, check this … Read more
Understanding NSRunLoop
A run loop is an abstraction that (among other things) provides a mechanism to handle system input sources (sockets, ports, files, keyboard, mouse, timers, etc). Each NSThread has its own run loop, which can be accessed via the currentRunLoop method. In general, you do not need to access the run loop directly, though there are … Read more
NSDefaultRunLoopMode vs NSRunLoopCommonModes
A run loop is a mechanism that allows the system to wake up sleeping threads so that they may manage asynchronous events. Normally when you run a thread (with the exception of the main thread) there is an option to start the thread in a run loop or not. If the thread runs some sort … Read more