Reputation: 81
I am completely new to Swift GCD.Could someone explain the idea of a background threads in GCD? I would like to continuously fetch sensor data using a background thread(which should never be killed) and run UI and other things in the main thread(assuming the app is running in the foreground). Currently, I tried something like this (in the 'application' function of AppDelegate.swift):
dispatch_async(dispatch_get_global_queue(Int(QOS_CLASS_BACKGROUND.value), 0)) {
dispatch_async(dispatch_get_main_queue()) {
SensorCollection.sensorActivate(CMMotionManager())
}
}
SensorCollection : A sensor collection class I created. sensorActivate : A class function. CMMotionManager() : The in-built swift motion manager.
Upvotes: 0
Views: 497
Reputation: 534925
I would like to continuously fetch sensor data using a background thread
That is what built-in CMMotionManager calls all let you do, so simply use them and stop trying to manage the threads yourself.
For example, startGyroUpdatesToQueue:withHandler:
lets you specify a queue. startMagnetometerUpdatesToQueue:withHandler:
lets you specify a queue. And so on.
So simply create a queue, store it in a property, and specify that queue when you start updating. Problem solved. Don't make this more complicated than it has to be by adding your own threading when you don't know what you are doing.
Upvotes: 1