ios - 成功完成 API 调用 SWIFT 后不显示警报视图控制器
问题描述
我有以下 API 调用:
func updateSheetVals() {
let args = ["id": viewModel.id]
let measurements = UpdateRequest.Measurements(
departure: Double(sheet.postRefuelQuantity.field.currentValue),
discrepancy: Double(sheet.discrepancy.field.currentValue),
discrepancyPercentage: Double(sheet.discrepancyPercent.field.currentValue),
preTotal: Double(dheet.preTotal.field.currentValue),
targetTotal: Double(fuelSheet.requiredTotal.field.currentValue)
)
let body = UpdateRequest(measurement: measurements)
API.client.post(.updateFuelSheetVals, with: args, using: .put, posting: body, expecting: MessageResponse.self) { (success, response) in
switch success {
case .failure:
print("Check network connection")
case .success:
DispatchQueue.asyncMain {
self.present(WarningViewController.finished(), animated: true)
}
}
}
}
}
然而,即使我收到 200 响应并且 API 被正确调用,我的视图控制器也永远不会显示。如果需要,很高兴提供更多上下文代码,但首先想知道我是否只是错过了这个块明显的东西......
编辑:
API调用在以下代码中触发:
func acceptButtonPressed(_ button: Button) {
var confirmation: UIViewController & ConfirmationAction
guard let level = viewModel.getSelectedSheet().order.levelDouble else { return }
if self.viewModel.requiresSignature {
if level < 3 {
confirmation = SignatureViewController(hasDiscrepancy: viewModel.hasDiscrepancy, discrepancyPrompt: viewModel.discrepancyPrompt, sl: level)
} else {
confirmation = SignatureViewController(hasDiscrepancy: viewModel.hasDiscrepancy,
discrepancyPrompt: viewModel.discrepancyPrompt, sl: level)
}
} else {
if let userInputAllowed = sheet.userInputAllowed, level < 3, !userInputAllowed {
confirmation = OrderAcceptAlertViewController.alert()
} else if level < 3 {
confirmation = DiscrepancyAlertViewController.alertWithDiscrepancy(hasDiscrepancy: viewModel.hasDiscrepancy,
discrepancyPrompt: viewModel.discrepancyFromManualInput(discrepancyValue: fuelSheet.percentageDiscrepancy.field.currentValue))
} else {
confirmation = DiscrepancyAlertViewController.alertWithDiscrepancy(hasDiscrepancy: viewModel.hasDiscrepancy,
discrepancyPrompt: viewModel.discrepancyPrompt)
}
}
confirmation.confirmationAction = { [weak confirmation, weak self] response in
guard let self = self else {
return
}
var completedSignature: SignatureParameter?
switch response {
case let .signature(signature):
completedSignature = signature
case .discrepancy:
break
}
let args = ["id": self.viewModel.id]
let params = AcceptParameter(
employee: self.viewModel.employee,
signature: completedSignature,
base64FuelSheet: self.sheet.ticket?.base64
)
if let confirm = confirmation {
confirm.setLoading(true)
API.client.post(.accept, with: args, using: .put, posting: params, expecting: Nothing.self, completion: { [weak self] (success, _) in
DispatchQueue.asyncMain {
guard let self = self else {
return
}
confirm.setLoading(false)
self.navigationController?.popViewController(animated: true)
}
}
})
self.updateSheetVals()
}
}
present(confirmation, animated: true, completion: nil)
}
解决方案
这肯定会起作用:
case .success:
DispatchQueue.asyncMain {
let viewController = self.storyboard?.instantiateViewController(withIdentifier: WarningViewController) as! WarningViewController
self.present(viewController, animated: true, completion: nil)
}
}
推荐阅读
- reactjs - 使用 React 应用程序删除 firebase 中的数据
- java - Spring Boot 应用程序在 systemd 单元中设置特定的 java 版本
- javascript - 如何在不对父元素应用更改的情况下将悬停类应用于任何元素?
- docker - 在特定路径上安装 docker 卷
- asp.net - 使用 asp.net 返回带有 json 响应的特定视图
- c++ - 在 C++ 的此构造函数警告中未初始化成员“xyz”
- python - Pandas:TypeError:字符串索引必须是整数
- docker - Docker 改变 Filesystem 和 Native Overlay Diff
- variable-assignment - CAPL:创建一个查找表,将值分配给变化的变量
- c - 为什么我不能将 void 和参数传递给 C 中的函数?