Alamofire 5 swift 5 等待alamofire查询结果显示初始视图
Alamofire 5 swift 5 wait for the result of the alamofire query to show the initial view
目前的情况是我有登录的初始视图 (A) 和 (B) 已经登录时的第二个视图。
我希望当我打开应用程序并且用户将值存储在 UserDefaults.standard 中时,检查这些值是否与服务器的值匹配,如果它们正确则进入 B 视图。
问题是 alamofire 是异步的,在你得到答案之前就加载了 A-view。
SceneDelegate.swift
第一次尝试才知道alamofire是异步启动的
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
//Code to open default viewA
...
//Code to try open viewB
let logAcepted = openViewB(url, param)
if logAcepted //<-- this execute before query in func before
{
if let windowScene = scene as? UIWindowScene
{
let window = UIWindow(windowScene: windowScene)
window.rootViewController = UIHostingController(rootView: Principal())
self.window = window
window.makeKeyAndVisible()
}
}
}
我看到有一个补全字段可以帮助解决这个问题,但我认为我没有很好地应用它。
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
//Code to open default viewA
...
//Code to try open viewB
openViewB(url, param, completion: {logAcepted in
if logAcepted
{
if let windowScene = scene as? UIWindowScene
{
let window = UIWindow(windowScene: windowScene)
window.rootViewController = UIHostingController(rootView: Principal())
self.window = window
window.makeKeyAndVisible()
}
}
})
}
如何使此功能 运行 同步?还是让主代码等待查询结果?
func openViewB(_ url: String, _ param: [String : String], completion : @escaping (Bool)->()) {
var ret : Bool = false
AF.request(url, parameters: param).responseJSON {response in
switch response.result {
case .success(let value):
if let JSON = value as? [String: Any]
{
let status = JSON["status"] as! String
switch status
{
case "Ok":
ret = true
completion(ret)
default:
ret = false
completion(ret)
}
}
case .failure( _):
ret = false
completion(ret)
}
}
}
我找到了很多相关信息,但是 none 在这个版本的 swift/alamofire
中有效
不要等待,永远不要等待。
您的 openViewB
方法已经有一个完成处理程序,请使用它。
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
//Code to open default viewA
...
//Code to try open viewB
openViewB(url, param) { logAccepted in
if logAccepted
{
if let windowScene = scene as? UIWindowScene
{
let window = UIWindow(windowScene: windowScene)
window.rootViewController = UIHostingController(rootView: Principal())
self.window = window
window.makeKeyAndVisible()
}
}
}
}
目前的情况是我有登录的初始视图 (A) 和 (B) 已经登录时的第二个视图。
我希望当我打开应用程序并且用户将值存储在 UserDefaults.standard 中时,检查这些值是否与服务器的值匹配,如果它们正确则进入 B 视图。
问题是 alamofire 是异步的,在你得到答案之前就加载了 A-view。
SceneDelegate.swift
第一次尝试才知道alamofire是异步启动的
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
//Code to open default viewA
...
//Code to try open viewB
let logAcepted = openViewB(url, param)
if logAcepted //<-- this execute before query in func before
{
if let windowScene = scene as? UIWindowScene
{
let window = UIWindow(windowScene: windowScene)
window.rootViewController = UIHostingController(rootView: Principal())
self.window = window
window.makeKeyAndVisible()
}
}
}
我看到有一个补全字段可以帮助解决这个问题,但我认为我没有很好地应用它。
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
//Code to open default viewA
...
//Code to try open viewB
openViewB(url, param, completion: {logAcepted in
if logAcepted
{
if let windowScene = scene as? UIWindowScene
{
let window = UIWindow(windowScene: windowScene)
window.rootViewController = UIHostingController(rootView: Principal())
self.window = window
window.makeKeyAndVisible()
}
}
})
}
如何使此功能 运行 同步?还是让主代码等待查询结果?
func openViewB(_ url: String, _ param: [String : String], completion : @escaping (Bool)->()) {
var ret : Bool = false
AF.request(url, parameters: param).responseJSON {response in
switch response.result {
case .success(let value):
if let JSON = value as? [String: Any]
{
let status = JSON["status"] as! String
switch status
{
case "Ok":
ret = true
completion(ret)
default:
ret = false
completion(ret)
}
}
case .failure( _):
ret = false
completion(ret)
}
}
}
我找到了很多相关信息,但是 none 在这个版本的 swift/alamofire
中有效不要等待,永远不要等待。
您的 openViewB
方法已经有一个完成处理程序,请使用它。
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
//Code to open default viewA
...
//Code to try open viewB
openViewB(url, param) { logAccepted in
if logAccepted
{
if let windowScene = scene as? UIWindowScene
{
let window = UIWindow(windowScene: windowScene)
window.rootViewController = UIHostingController(rootView: Principal())
self.window = window
window.makeKeyAndVisible()
}
}
}
}