For timer related operations is it worth using any QOS? If yes then which thread should be used or which QOS can we use?
Now here I am creating a Timer and adding it to Runloop:
var activityTimer = Timer.scheduledTimer(timeInterval: 5, target: self, selector: #selector(self.runActivity), userInfo: nil, repeats: true)
RunLoop.current.add(activityTimer!, forMode: .common)
I have created global object of DispatchQueue with QOS .userInteractive:
let queue = DispatchQueue(label: "com.example.timer", qos: .userInteractive)
Here I am sending data to API:
@objc func runActivity () {
queue.async {
self.postDataUsingAPI()
}
}
So which QOS should be appropriate to use here? I want all these activities running in the background only.
I think we can agree that
userInteractiveis wrong, as user interactive is exactly what this code is not. By using a background serial queue you are already saying you don't really care exactly when this code runs. And a timer is already approximate; that is, you are not going to be callingrunActivityat exact time intervals either. On the other hand the queue is serial so you won't miss or overlap any calls. Finally, you are going to be networking which means you've thrown away all hope of rapid response, as you are using the totally unpredictable network. Thus.defaultor.utility, or unspecified, is far more appropriate.But I would also question your use of a background queue at all. The timer is already effectively running in the background. Moreover, networking itself is already in the background, and your queue won't affect that in any way. I think you should throw your background queue away and let the networking do its usual work in the usual way. The place to worry about what queue you're on is when you get the callback from the network, and that is something you specify in a totally different way.
By the way, your
RunLoop.current.addis wrong. Delete it. A scheduled timer is already added to the run loop for you; that is exactly whatscheduledmeans.