Dispatch

RSS for tag

Execute code concurrently on multicore hardware by submitting work to dispatch queues managed by the system using Dispatch.

Dispatch Documentation

Pinned Posts

Posts under Dispatch tag

28 Posts
Sort by:
Post marked as solved
8 Replies
3.1k Views
I see this warning when my app runs: Thread running at QOS_CLASS_USER_INTERACTIVE waiting on a lower QoS thread running at QOS_CLASS_DEFAULT. Investigate ways to avoid priority inversions This is true; I know what is going on. I'd like this other thread to have a higher priority. But how do I set the "QOS class" for a thread? Searching developer.apple.com for QOS_CLASS_USER_INTERACTIVE doesn't find much. It seems that dispatch queues have priorities, but in this case I have a thread, not a dispatch queue. Any ideas?
Posted
by endecotp.
Last updated
.
Post marked as solved
2 Replies
616 Views
Below is some simple code that does nothing, saves nothing. Yet the memory usage keeps rising. Am I missing something or is this a bug? The code requires 6 core CPU minimum. ContentView.swift import SwiftUI import Combine struct ContentView: View { @EnvironmentObject var loopClass:LoopClass var body: some View { ProgressView(value: Float(loopClass.loop_count), total: Float(loopClass.max_loops) ).progressViewStyle(.circular).padding() Button("Run looper", action: { loopClass.loopFunc() } ) } } LoopClass.swift import Combine class LoopClass: ObservableObject { @Published var loop_count = 0 @Published var max_loops = 0 func loopFunc () { let loopSet = [ 1, 3, 5, 7, 11, 13 ] max_loops = Int(pow(Double(loopSet.count), 13.0)) print("max_loops = \(max_loops)") for loop13 in loopSet { DispatchQueue.global().async { for loop12 in loopSet { for loop11 in loopSet { for loop10 in loopSet { for loop9 in loopSet { for loop8 in loopSet { for loop17 in loopSet { for loop16 in loopSet { for loop5 in loopSet { for loop4 in loopSet { for loop3 in loopSet { for loop2 in loopSet { for loop1 in loopSet { DispatchQueue.main.async{ self.loop_count += 1 } } }}}}}}}}}}} } // DQ } // for loop13 } }
Posted Last updated
.
Post not yet marked as solved
0 Replies
1.4k Views
In What's New In Swift, a new DispatchSerialQueue-backed actor was introduced. We're able to call MainActor-annotated functions using DispatchQueue.main.async without errors or warnings. For example: @MainActor func randomFunc() { print("Hello World") } DispatchQueue.main.async { randomFunc() } However calling a globalActor-annotated function or a regular actor-isolated function backed by a DispatchSerialQueue, we get the warning Actor-isolated instance method 'randomFunc()' can not be referenced from a non-isolated context; this is an error in Swift 6. Code here: actor MyActor { private let queue: DispatchSerialQueue nonisolated var unownedExecutor: UnownedSerialExecutor { queue.asUnownedSerialExecutor() } init(queue: DispatchSerialQueue) { self.queue = queue } func randomFunc() { print("Hello World!") } } let queue = DispatchSerialQueue(label: "actorQueue") let actor = MyActor(queue: queue) queue.async { actor.randomFunc() // Warning here } Although it has a warning, the code still runs successfully and prints Hello World, but it would also do so from another DispatchQueue not used by the actor (this was tested in Version 15.0 beta (15A5160n) Playground). My question: Can we remove the warning resulting from calling an actor isolated function backed by DispatchSerialQueue A using A.async { }, if that's safe behavior? If it's not safe, why not?
Posted
by achouman.
Last updated
.
Post marked as solved
1 Replies
1.2k Views
Do run loops use an operation queue under the hood? When I dispatch to the mian queue, I know it will run on the mian thread, which means it will be handled b the main thread's run loop. But is the other way around correct? More specific question, is it possible somehow that the following code will return true? OperationQueue.current != OperationQueue.main && Thread.isMainThread I tried to do for example to dispatch to the main thread using a timer. myQueue.sync { let timer = Timer(timeInterval: 1, repeats: false) { _ in let curr = OperationQueue.current let main = OperationQueue.main print("\(String(describing: curr)) \(main)") } RunLoop.main.add(timer, forMode: .common) } And got the same pointer. But maybe some other way of dispatching to the main run loop will give different results, possibly that is why OperationQueue.current can return nil.
Posted
by artium.
Last updated
.
Post not yet marked as solved
1 Replies
863 Views
Experiencing an increased amount of crash reports with the latest release of the app. I've spent some time online understanding the meaning of Unbalanced call to dispatch_group_leave() however, I am struggling to pinpoint what's the source of the problem inside the codebase. I wish you can help me with these crash logs. Will calling a closure from inside CoreData context's perform() will eventually result into this crash and cause leave() to get called multiple times? The main code was there for long time, the only recent change was to wrap some code around CoreData context perform() or performAndWait() to avoid concurrency issues. 2023-05-20_10-42-03.5467_+0100-e8b3d9b142b8fd3d11c72abc01ad2fbcfbe92aee.crash 2023-05-22_20-57-32.2620_+0400-5d4c76afe1e42937cf09035d3034fb33ec40f0c9.crash 2023-05-21_19-36-18.3480_+0100-c01f62a2b78cae616c6202940f06220b7ea3b760.crash 2023-05-16_15-29-43.3962_+0100-ebb44a6156a3771271acfdd459546fe1271eb370.crash
Posted
by EleFanta.
Last updated
.
Post marked as solved
4 Replies
876 Views
I'm running a simulation (SwiftUI app), which has 100 steps. I need each step to be executed in order. A first try was to dispatch with delay to schedule each second: for step in 0..<100 { DispatchQueue.main.asyncAfter(deadline: .now() + Double(step) * 1.0) { // simulation code } } Very poor results as 100 running threads are too much load for the system. So I split in 2 stages: for bigStep in 0..<10 { DispatchQueue.main.asyncAfter(deadline: .now() + Double(bigStep) * 10.0 ) { for step in 0..<10 { DispatchQueue.main.asyncAfter(deadline: .now() + Double(step) * 1.0) { // simulation code } } } } It works much better, as now there are a max of 20 threads active (in fact I create more levels to limit to a max of 8 concurrent threads). It addition, it allows to interrupt the simulation before end. My questions: is it the appropriate pattern ? Would a timer be better ? Other options ?
Posted
by Claude31.
Last updated
.
Post not yet marked as solved
0 Replies
715 Views
I understand UI updates have to be on the main thread, but what about SKSpriteNode's texture? Can I update that in the background? I can update it in the background and it works like I want it to. No crashes, warnings, etc. I did verify that the thread is not the main thread via [NSThread isMainThread]. I have since wrapped the texture change in a dispatch_sync on the main queue and it works the same as without it. Regards, Patrick
Posted
by PatrickM.
Last updated
.