Questions tagged [dispatch-queue]

165 questions
370
votes
16 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
  • 3,894
  • 3
  • 10
  • 11
46
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
  • 935
  • 1
  • 8
  • 22
32
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,157
  • 4
  • 25
  • 40
10
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 Brower
  • 1,264
  • 1
  • 11
  • 41
10
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,022
  • 9
  • 26
8
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,232
  • 10
  • 18
7
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…
6
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
  • 5,850
  • 8
  • 45
  • 66
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…
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…
Cuong Vuong
  • 164
  • 6
4
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…
4
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…
4
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
  • 95
  • 1
  • 1
  • 7
4
votes
1 answer

Applying filter to real time camera preview - Swift

I'm trying to follow the answer given here: https://stackoverflow.com/a/32381052/8422218 to create an app which uses the back facing camera and adds a filter, then displays it on the screen in real time here is my code: // // …
nopassport1
  • 1,747
  • 13
  • 39
3
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
  • 31
  • 3
  • 13
1
2 3
10 11