Questions tagged [dispatch-queue]
231 questions
419
votes
17 answers
How to use background thread in swift?
How to use threading in swift?
dispatchOnMainThread:^{
NSLog(@"Block Executed On %s", dispatch_queue_get_label(dispatch_get_current_queue()));
}];

Anshul
- 4,386
- 3
- 12
- 11
70
votes
5 answers
Is DispatchQueue.global(qos: .userInteractive).async same as DispatchQueue.main.async
I was going through the tutorial :
https://www.raywenderlich.com/148513/grand-central-dispatch-tutorial-swift-3-part-1
And came across the definition of QoS class User-interactive.
Its mentioned there that this should run on main thread.
So, my…

Nishu_Priya
- 1,251
- 1
- 10
- 23
45
votes
1 answer
DispatchQueue : Cannot be called with asCopy = NO on non-main thread
I am presenting the UIAlertController on the main thread as :
class HelperMethodClass: NSObject {
class func showAlertMessage(message:String, viewController: UIViewController) {
let alertMessage = UIAlertController(title: "", message:…

Amit
- 4,837
- 5
- 31
- 46
20
votes
2 answers
Stop a DispatchQueue that is running on the main thread
I have this block of code:
DispatchQueue.main.asyncAfter(deadline: .now() + (delay * Double(isDelayAccounted.hashValue)) + extraDelay) {
self.isShootingOnHold = false
self.shoot()
self.shootingEngine =…

Pablo
- 1,302
- 1
- 16
- 35
13
votes
1 answer
Ambiguous use of 'dispatch_get_main_queue()'
How do I replace the following Swift code for iOS using the DispatchQueue class? This is old Swift 3 code that the newest Xcode won't convert to Swift 5.
dispatch_async(dispatch_get_main_queue()) { () -> Void in
// code
}
It is giving me an…

daniel
- 1,446
- 3
- 29
- 65
12
votes
2 answers
SWIFT - What's the difference between OperationQueue.main.addOperation and DispatchQueue.main.async?
Sometimes I must do something on the main thread and its suggested to place the code inside a OperationQueue.main.addOperation.
Other times, its suggested to write the code inside DispatchQueue.main.async.
What the difference between these…

Yuma Technical Inc.
- 623
- 11
- 28
12
votes
2 answers
Dealing with multiple completion handlers
I'm trying to coordinate several completion handlers for each element in an array.
The code is essentially this:
var results = [String:Int]()
func requestData(for identifiers: [String])
{
identifiers.forEach
{ identifier in
…

XmasRights
- 1,427
- 13
- 21
8
votes
2 answers
SwiftUI: How to only run code when the user stops typing in a TextField?
so I'm trying to make a search bar that doesn't run the code that displays the results until the user stops typing for 2 seconds (AKA it should reset a sort of timer when the user enters a new character). I tried using .onChange() and an AsyncAfter…

nickcoding
- 305
- 8
- 35
8
votes
2 answers
Does sync/async behave similar to serial/concurrent i.e., do they both control DispatchQueues or do sync/Async control Threads only
Most answers on stackoverflow implies in a way that sync vs async behaviour is quite similar to serial vs concurrent queue concept difference. Like the link in the first comment by @Roope
I have started to think that
Serial and concurrent are…

Aron
- 91
- 5
7
votes
3 answers
Swift Cancel DispatchQueue Process
I have a UDP method that waits for a reply using the DispatchQueue using the following code:
DispatchQueue.global(qos: .userInitiated).async {
let server:UDPServer=UDPServer(address:"0.0.0.0", port:5005)
let (data,_,_) = server.recv(1024)
…

iphaaw
- 6,764
- 11
- 58
- 83
7
votes
2 answers
Difference between DispatchQueue types in swift
As I understand there are 3 types of DispatchQueue in swift:
Main (serial) (Main Thread)
Global (Concurrent) (Background Threads working in parallel)
Custom (Concurrent or serial)
And each one maybe work (asynch or synch)
First…

Saeed Alasiry
- 322
- 5
- 23
7
votes
3 answers
how to stop a dispatchQueue in swift
I have a DispatchQueue for a introViewController that shows a gif for 11 seconds and then display my login page... But also have a button that skip the intro and display the login. When I click it the time still running and when I am navigating in…

Kevin Arvizu
- 135
- 1
- 1
- 7
5
votes
0 answers
iOS CoreBluetooth dispatch queue for app background processing
First of all the question what is the best way of using core bluetooth in the central role to send data to a bluetooth LE devices. The data need to be processed and that takes enough time to cause problems on the UI thread if it runs on it. The user…

Marc
- 1,159
- 17
- 31
5
votes
2 answers
Why a sync block of code always call on main thread?
I did the simple test with DispatchQueue:
DispatchQueue.global(qos: .background).sync {
if Thread.isMainThread {
print("Main thread")
}
}
It printed out:
Main thread
Why does this code execute on the main thread? It should be performed on…

Vuong Cuong
- 192
- 9
4
votes
4 answers
Type of expression is ambiguous without more context error
How do I fix this code so it doesn't give the error: Type of expression is ambiguous without more context?
var int = 0
while int < 100 {
DispatchQueue.main.asyncAfter(deadline: .now() + int) { // change 2 to desired number of seconds
…

D-A UK
- 1,084
- 2
- 11
- 24