Create background run in iOS

Use case

I have a set of processes, each of which should start in its own background thread with a certain execution speed (3 times per second, every 10 seconds, etc.).

I believe CFRunLoop and / or NSRunLoop provide this functionality

Question

How to create (in swift) a new background thread to perform periodic tasks?

+5
source share
2 answers

This can be done using the Global Dispatch background queue.

Background thread example:

  let qualityServiceClass = QOS_CLASS_BACKGROUND let backgroundQueue = dispatch_get_global_queue(qualityServiceClass, 0) dispatch_async(backgroundQueue, { print("This is run on the background queue") // With the help of NSTimer it will hit method after every 5 minutes (300 seconds). _ = NSTimer.scheduledTimerWithTimeInterval(300.0, target: self, selector: #selector(AppDelegate.callAppSettingWebservice), userInfo: nil, repeats: true) }) 
+1
source

Old post, but the "background run loop" is not equal to the "background thread" and does not mean "background queue". Usually there is only one run loop, and this is the run loop of the main thread. Other threads typically use this run loop, but rarely have their own run loop (or their respective run loops will never be easily accessible). To create a run loop for a thread that is not the main thread, Apple has a guide and tells you when it should be done.

There is also a good guide on run loops in background threads.

0
source

Source: https://habr.com/ru/post/1233621/


All Articles