Questions tagged [dispatch-queue]
231 questions
2
votes
1 answer
Proper way to end an Async process
So I have this code:
@IBAction func onStart(_ sender: Any) {
DispatchQueue(label: "SyncQueue").async{
for i in 0..

SquareBox
- 823
- 1
- 10
- 22
1
vote
2 answers
Why can DispatchQueue.main.async be used as an input source for RunLoop.current.run?
import Dispatch
import Foundation
DispatchQueue.main.async {
print("just print in main async")
}
RunLoop.current.run(mode: .default, before: .distantFuture)
print("RunLoop.current.run ends!")
If the runLoop ends after printing "just print…

eczn
- 1,459
- 2
- 10
- 15
1
vote
1 answer
Wrap async task into DispatchWorkItem in Swift to make it cancellable?
I have a class with async methods. All the methods are the simplest:
class SomeClass {
func someFunc(params: SomeParams, completion: ((SomeResult) -> ())?) {
... //some code where completion is called asynchronously, not only with…

Gargo
- 1,135
- 1
- 10
- 21
1
vote
1 answer
how to use a construct await sync inside a closure
I have a code that loads data from firebase, the data is not loaded immediately and I want to load the data first, and then pass the data to the delegate.
To do this, I used the wait construct and the synchronous loadData function
let dateUser =…

Denim
- 9
- 2
1
vote
1 answer
Swift concurrency tasks vs dispatch queue threads: What dictates how many tasks are running simultaneously?
For this swift playground (view on swiftfiddle or below), I have three loops of process-intensive work. The one that uses threads spawns 50 threads that all work together (shown by the increasing # of loops reported at the end), and the ones with…

David
- 95
- 1
- 7
1
vote
1 answer
Is it safe to call DispatchQueue sync for read operations?
I have a class that reads from and writes to a dictionary using a serial dispatch queue. While the write operations are asynchronous, the read operations are synchronous. I want to avoid the use of a completion handler for reading. Does this create…

DesperateLearner
- 1,115
- 3
- 19
- 45
1
vote
1 answer
Proper iOS DispatchQueue usage?
I'm trying to extend the Landmark SwiftUI Tutorials to persist data.
The code I added getDataFromFileURL draw inspiration from Persisting data tutorial.
I expect for the first time, the json files won't exist in the user document directory, and the…

Patrick
- 4,186
- 9
- 32
- 45
1
vote
1 answer
Serial DispatchQueue is not working as expected
To load some information in my app's view, I need it to finish networking because some methods depend on the result. I looked into serial DispatchQueue and .async methods, but it's not working as expected.
Here is what I tried so far. I defined 3…

Shakiba
- 11
- 2
1
vote
1 answer
How to move code block out of main thread at SwiftUI
I need to fetch contacts at application launch and store them with Core Data.
For fetching I have following method:
static var fetchAllContacts: PersistenceController = {
let result = PersistenceController(inMemory: true)
let…

dehelden
- 39
- 6
1
vote
0 answers
DispatchSemaphore + DispatchQueue not working as expected?
I'm trying to improve the time it takes for a task to finish by leveraging multithreading/paralleling.
I'm reading CMSampleBuffers from a video track, manipulating them, then storing them back to an array for later use. Because each manipulation is…

Roi Mulia
- 5,626
- 11
- 54
- 105
1
vote
1 answer
Is `DispatchQueue.main.async` block in viewWillAppear always called after `viewDidLayoutSubviews`?
I wanted to change the collection view's contentOffset.x right after pushing VC.
So I called collectionView.setContentOffset(~) in viewWillAppear.
But It didn't work because of auto layout cycle.
However, if I call collectionView.setContentOffset…

Nala
- 37
- 5
1
vote
1 answer
Having issue with DispatchQueue not working after 3-4 executions in Swift
I have the following code below to show a Message to the user and then disappear. Everything works fine for the first 1 to 3 tries, but anything above that the Message window does not disappear as it did on the previous attempts/required.
struct…

Learn2Code
- 1,974
- 5
- 24
- 46
1
vote
0 answers
Task with high quality-of-service submitted to low QoS queue
I'm learning Swift concurrency and encountered this excerpt
“If you submit a task with a higher quality of service than the queue has, the queue’s level will increase. Not only that, but all the operations enqueued will also have their priority…

chlkdst
- 175
- 3
- 13
1
vote
1 answer
Which one will execute first if we submit two tasks to same dispatchqueue?
Created Custom dispatch queue and submitted two tasks to same queue and I gave sleep(3) for first task and sleep(1) for second task. then why first task completes execution first?
let queue = DispatchQueue(label: "name");
queue.async {
…

Smitha
- 33
- 2
1
vote
2 answers
Swift weird behavior with multithreading dispatch queues
I am trying to use dispatch queues in swift, but some weired beheviour appeared when calling the async function. In the following code I called the function "fun" 10 times each time with the .async function, and as I know from other languages that…

Ahmad_Hamad
- 41
- 3