gpt4 book ai didi

ios - 无法在调度队列 swift 3 的主线程中更新 UI

转载 作者:行者123 更新时间:2023-11-28 12:17:04 24 4
gpt4 key购买 nike

我正在使用 Alamofire 和 SVProgressHUD 来查询 API 和处理数据。问题是在 dispatchqueue 里面,即使我在主线程中更新 UI,更新的结果也会在 dispatch queue 完成后出现。 我想要做的是在进度条上显示一个进度状态,并在一个循环中不断更新进度状态(float)。但是,在 dispatchqueue 中的所有事情都完成之前,这不会出现。我的代码如下所示。干杯

DispatchQueue.global(qos: .userInitiated).async(execute: {
Alamofire.request(queryURL).responseJSON() { response in
//Do something
for i in 0..<100{
//Do lots of things
DispatchQueue.main.async {
SVProgressHUD.showProgress(Float(i/100), status: "Loading...")
}
}
//Dismiss
})

最佳答案

试试下面的代码:-

DispatchQueue.global(qos: .userInitiated).async(execute: {
Alamofire.request(queryURL).responseJSON() { response in
//Do something
for i in 0..<100{
//Do lots of things
DispatchQueue.main.async {
SVProgressHUD.showProgress(Float(i)/100 as Float, status: "Loading...")
}
}
//Dismiss
})

它将显示为......

0.00.010.020.030.040.050.060.070.080.090.10.110.120.130.140.150.160.170.180.190.20.210.220.230.240.250.260.270.280.290.30.310.320.330.340.350.360.370.380.390.40.410.420.430.440.450.460.470.480.490.50.510.520.530.540.550.560.570.580.590.60.610.620.630.640.650.660.670.680.690.70.710.720.730.740.750.760.770.780.790.80.810.820.830.840.850.860.870.880.890.90.910.920.930.940.950.960.970.980.99

关于ios - 无法在调度队列 swift 3 的主线程中更新 UI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46097304/

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