我想在收到服务器的回复后更改我的标签文字,但在收到服务器的回复后它没有改变!!
这是我的代码:
func getOrderCost() {
DispatchQueue.main.async {
self.customView?.setOrderPriceEstimate(price: "۰")
var price = 10
if let p_code = self.customView?.getPromotionCode() {
self.orderPriceObject.promotion_code = p_code
}
self.registerOrderVM.getOrderPriceEstimation(orderInfo: self.orderPriceObject).asObservable().subscribe(onNext: { (response) in
if let res = response as? [String: Int], let p = res["price_with_added_value"] {
self.customView?.setOrderPriceEstimate(price: "cost : \(p)")
}
}, onError: { (error) in
self.view.showMessage(error.localizedDescription, type: .error, options: [.hideOnTap(true)])
}).disposed(by: self.disposeBag)
}
}
这是setOrderPriceEstimate
中的customView
函数:
public func setOrderPriceEstimate(price: String) {
self.orderCostLbl.text = price
}
答案 0 :(得分:0)
使 DispatchQueue.main.async 外部并不保证 getOrderPriceEstimation 中的代码将在主队列中运行,因为getOrder中的代码可能位于其他线程中
func getOrderCost() {
DispatchQueue.main.async {
self.customView?.setOrderPriceEstimate(price: "۰")
var price = 10
if let p_code = self.customView?.getPromotionCode() {
self.orderPriceObject.promotion_code = p_code
}
self.registerOrderVM.getOrderPriceEstimation(orderInfo: self.orderPriceObject).asObservable().subscribe(onNext: { (response) in
if let res = response as? [String: Int], let p = res["price_with_added_value"] {
DispatchQueue.main.async {
self.customView?.setOrderPriceEstimate(price: "cost : \(p)")
}
}
}, onError: { (error) in
self.view.showMessage(error.localizedDescription, type: .error, options: [.hideOnTap(true)])
}).disposed(by: self.disposeBag)
}
}
验证是否存在p
if let res = response as? [String: Int], let p = res["price_with_added_value"] {
DispatchQueue.main.async {
self.customView?.setOrderPriceEstimate(price: "cost : \(p)") }
}
else
{
print("res or p is not found")
}
同时检查customView是否为nil
答案 1 :(得分:0)
DispatchQueue.main.async {
self.customView?.setOrderPriceEstimate(price: "cost : \(p)")
}
我认为你必须在主线程中更新UI。