Swift Alamofire 异步问题

Swift Alamofire Async Issue

这里是 swift 的新手。

我正在尝试在另一个 AF.request 调用中进行 AF.request 调用,一切正常。

问题是 fetchAllUsers() 在所有内容加载完成后被调用。因此,我没有立即获取所有用户,而是必须刷新页面才能执行 fetchAllUsers()

我认为使用闭包可以避免这个问题,但它仍然发生。

我是不是做错了什么?

func fetchAllUsers(completionHandler: @escaping ([User]) -> Void) {
        let allUsersUrl = baseUrl + "users/"
        let headers: HTTPHeaders = [
            .authorization(bearerToken: NetworkManager.authToken)
        ]
        
        if let url = URL(string: allUsersUrl) {
            AF.request(url, headers: headers).responseDecodable(of: [User].self) { response in
                if let users = response.value {
                    completionHandler(users)
                } else {
                    print("Users is empty")
                }
            }
        } else {
            print("URL error")
        }
    }

 func login(param: [String:String], completionHandler: @escaping (Int) -> Void) {
    let loginUrl = baseUrl + "auth/login"        
    
    // Sets the Logged In User
    AF.request(loginUrl, method: .post, parameters: param, encoding: JSONEncoding.default, headers: nil).responseString { response in
        if let data = response.data {
            let authTokenString = String(decoding: data, as:UTF8.self).components(separatedBy: " ")
            
            // Sets the authentication token
            NetworkManager.authToken = authTokenString[1]
            
            self.fetchAllUsers { users in
                for user in users {
                    if param["username"] == user.username {
                        HomeViewModel.loggedInUser = user
                    }
                }
            }
                            
            completionHandler(response.response!.statusCode)
        }
    }
}

将 main 函数的完成处理程序和状态代码传递给 fetchAllUsers 并在它自己的完成处理程序之后在那里调用它

completionHandler(response.response!.statusCode)self.fetchAllUsers closure 之前被执行,因为它正在等待for api 响应完成。 completionHandler(response.response!.statusCode) 在执行之前破坏了 self.fetchAllUsers 闭包,所以我调用了 completionHandler(response.response!.statusCode) inside self.fetchAllUsers 关闭后

 completionHandler(users)
 mainCompletion(statusCode)  

完整代码工作如下

func fetchAllUsers(statusCode:Int, mainCompletion: @escaping (Int) -> Void, completionHandler: @escaping ([User]) -> Void) {
        let allUsersUrl = baseUrl + "users/"
        let headers: HTTPHeaders = [
            .authorization(bearerToken: NetworkManager.authToken)
        ]
    
        if let url = URL(string: allUsersUrl) {
            AF.request(url, headers: headers).responseDecodable(of: [User].self) { response in
                if let users = response.value {
                    completionHandler(users)
                    mainCompletion(statusCode)
                } else {
                        print("Users is empty")
                }
            }
        } else {
            print("URL error")
        }
    }

func login(param: [String:String], completionHandler: @escaping (Int) -> Void) {
    let loginUrl = baseUrl + "auth/login"        

    // Sets the Logged In User
    AF.request(loginUrl, method: .post, parameters: param, encoding: JSONEncoding.default, headers: nil).responseString { response in
        if let data = response.data {
            let authTokenString = String(decoding: data, as:UTF8.self).components(separatedBy: " ")
        
            // Sets the authentication token
            NetworkManager.authToken = authTokenString[1]
        
            self.fetchAllUsers(statusCode: response.response!.statusCode, mainCompletion: completionHandler) { users in
                for user in users {
                    if param["username"] == user.username {
                        HomeViewModel.loggedInUser = user
                    }
                }
            }
                        
            
        }
    }
}