快速REPL是否支持异步DispatchQueue?

问题描述 投票:0回答:1

我正在swift中并发进行修改,并且怀疑REPL并没有像在实时应用程序中那样实际运行DispatchQueue。我已经从包括以下问题在内的几个问题中复制并粘贴了代码:

let group = DispatchGroup()
let queue = DispatchQueue.global(qos: .default)

for i in 1...4 {
    queue.async(group: group) {
        print("🔹 \(i)")
    }
}

for i in 1...4 {
    queue.async(group: group) {
        print("❌ \(i)")
    }
}

group.notify(queue: .main) {
    print("jobs done by group")
}

现在将其粘贴到repl中时,我看到一些旧队列项目和一些新队列项目,但是没有看到[[all预期的新项目。

🔹 2 🔹 4 image video 🔹 3 group: DispatchGroup = { baseOS_dispatch_object@0 = { baseOS_object@0 = { baseNSObject@0 = { isa = OS_dispatch_group } } } } queue: OS_dispatch_queue_global = { baseOS_dispatch_queue@0 = { baseOS_dispatch_object@0 = { baseOS_object@0 = { baseNSObject@0 = { isa = OS_dispatch_queue_global } } } } }
然后我输入一些随机的附加语句..,并查看完成的其他排队任务:

96> let x = 3 ❌ 1 x: Int = 3 97> let x = 4 video x: Int = 4 98> x ❌ 2 $R1: Int = 4

似乎很明显正在运行

no

个后台线程。有没有办法在repl中获得真正的并发/线程操作?
swift multithreading read-eval-print-loop
1个回答
0
投票
[确定,这很容易-关于quality of service。更改为userInteractive

let dispatchQueue = DispatchQueue.global(qos: .userInteractive)

我们现在有了预期的结果:

🔹 2 🔹 4 ❌ 4 🔹 1 ❌ 3 ❌ 1 🔹 1 🔹 3 🔹 3 🔹 4

这里是完全正确的代码

let group = DispatchGroup() let dispatchQueue = DispatchQueue.global(qos: .userInteractive) for i in 1...4 { group.enter() dispatchQueue.async { print("🔹 \(i)") group.leave() } } for i in 1...4 { group.enter() dispatchQueue.async { print("❌ \(i)") group.leave() } } group.notify(queue: DispatchQueue.main) { print("jobs done by group") }

© www.soinside.com 2019 - 2024. All rights reserved.