gpt4 book ai didi

带有同步 API 下载的 Swift HUD

转载 作者:行者123 更新时间:2023-11-28 10:34:37 27 4
gpt4 key购买 nike

我正在尝试在进行同步下载时向我的页面添加 HUD 效果。

如果我输入 viewDidLoad(),HUD 不会出现。

如果我输入 awakeFromNib(),屏幕会变暗,但不会出现进度微调器。 (我也尝试过 ViewWillAppear)

我正在使用 https://github.com/rjeprasad/RappleProgressHUD .

这在加载数据时没有影响。它仅在数据加载后立即起作用。

override func viewDidLoad() {
super.viewDidLoad()

RappleActivityIndicatorView.startAnimating()

// the API function
let a_Json = getDataJson(symbol: a)
let b_Json = getdDataJson(symbol: b)

RappleActivityIndicatorView.stopAnimating()

}

为 API 使用 Swift 4.2 和 Alamofire_Synchronous。

谢谢。

最佳答案

您需要在后台加载数据,以便 UI 可以保持运行。

这是您想要的一般模式:

// start the activity indicator here

DispatchQueue.global(qos: .background).async {
// Data loading / processing here

DispatchQueue.main.async {
// stop the activity indicator here and make any other UI updates
}
}

在您的具体情况下:

override func viewDidLoad() {
super.viewDidLoad()

RappleActivityIndicatorView.startAnimating()

DispatchQueue.global(qos: .background).async {
// the API function
let a_Json = getDataJson(symbol: a)
let b_Json = getdDataJson(symbol: b)

DispatchQueue.main.async {
RappleActivityIndicatorView.stopAnimating()
}
}
}

关于带有同步 API 下载的 Swift HUD,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54228592/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com