Swift 萌新, DispatchQueue 的一个问题

119 天前
 main1234

使用的 Xcode 16.2 、swift 6.0.3

代码如下

import Foundation

DispatchQueue.global().async {
    // 耗时操作(如网络请求、文件处理)
    let data = downloadData()
    
    DispatchQueue.main.async {
        updateUI(with: data)
    }
}

func downloadData() -> String{
    sleep(2)
    return "a"
}

func updateUI(with: Any) {
    print("updateUI")
}

sleep(10)

为什么我无法输出 updateUI

我应该如何在切换到主线程时候,让主线程输出 updateUI

2141 次点击
所在节点    Swift
17 条回复
kera0a
119 天前
主线程已经提前被 sleep(10)阻塞了,等 sleep 结束后程序估计也终止了
iOCZS
119 天前
主线程如何保持运行?
byby
119 天前
老方法用闭包 completion ,新方法用 async await
iyeatse
119 天前
把 `sleep(10)` 改成 `dispatchMain()`
MacsedProtoss
119 天前
main:|——————————( sleep 10)——————————|
dispatchGlobal. print(“updateUI”)
global: ——(sleep 2)——|
dispatchMain

为啥你要在主线程 sleep 阻塞住它呢?
MacsedProtoss
119 天前
@MacsedProtoss 奇怪…好像空格都被吃掉了,我的 format 全都失效了….
chiaf
119 天前
看描述,这好像是全部代码,没有 main 函数怎么运行🤪

如果是在 playground 里面,肯定能打印出来。
yoyoyoyolol
118 天前
你给的代码由于 sleep(10)卡住主线程了,导致 10 秒后才能打印 updateUI 。
因为 palyground 没有 runloop 环境,你要想实现 2 秒后打印的效果,可以这样写

import Foundation
import PlaygroundSupport // 关键导入

// 让 Playground 持续运行,不自动结束
PlaygroundPage.current.needsIndefiniteExecution = true

DispatchQueue.global().async {
// 耗时操作(如网络请求、文件处理)
let data = downloadData()

DispatchQueue.main.async {
updateUI(with: data)
}
}

func downloadData() -> String {
sleep(2)
return "a"
}

func updateUI(with: Any) {
print("updateUI")
}

// 不需要 sleep(10),Playground 会保持运行

其中 PlaygroundPage.current.needsIndefiniteExecution = true 这一句话可以让 palyground 开启 runloop 环境,避免了主线程使用 sleep 卡住线程
magic3584
118 天前
看着像是在 Playground 里。
建议创建一个项目在 viewController 里去调用
shixiaoda
118 天前
看到 iOS developer 后继有人,甚是欣慰
main1234
118 天前
@shixiaoda 老哥为啥这么说,IOS 开发不用 swift 了嘛
main1234
118 天前
@iOCZS 对,如何让主线程保持运行呢
main1234
118 天前
@chiaf 啊? swift 不也支持面向过程么
main1234
118 天前
@MacsedProtoss 萌新,我想让主线程不退出
MacsedProtoss
118 天前
@main1234 主线程不退出是 runloop 机制实现的 如果你用纯 swift cli 去跑那肯定不行
BeiChuanAlex
102 天前
@shixiaoda #10 哈哈哈,笑死了,宗门已经没落成这样了嘛
shixiaoda
93 天前
@main1234 iOS 开发肯定用 swift ,但 iOS 可能不用开发了 /狗头

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/1132529

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX