如何在异步调用 SwiftUI 后加载初始屏幕
How to load initial screen after an async call SwfitUI
在 AppDelegate class 中,我正在调用 Auth API 并且它 returns 令牌。然后我在 UserDefaults 中设置令牌。
然而此时我的 HomeView 正在打开,它的 .onAppear{} 函数调用另一个 api。我想等待 Auth API 结果的完成。之后要打开 HomeView。我该怎么做?
这也是授权或获取令牌的正确方式吗?
这是我的 AppDelegate、服务、HomeView:
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
Service.shared.authorize(userName: "username", password: "password") { (response) in
UserDefaults.standard.set(response.token, forKey: "Token")
}
return true
}
class Service {
func authorize(userName: String, password: String, completion: @escaping(AuthResponse>) -> ()) {
// Here I'm calling auth api
callApiAsync(urlRequest: urlRequest) { (apiResponse) in
completion(apiResponse)
}
}
}
struct HomeView: View {
@ObservedObject var vm = HomeViewModel()
var body: some View {
Text("HomeView")
.onAppear {
self.vm.getList() { response in
}
}
}
}
我不知道你在哪里初始化你的UIWindow
。 AppDelegate / SceneDelegate
我的 SceneDelegate
样本与 AppDelegate
相似。
请求完成后初始化您的window。
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
Service.shared.authorize(userName: "username", password: "password") { (response) in
UserDefaults.standard.set(response.token, forKey: "Token")
if let windowScene = scene as? UIWindowScene {
let window = UIWindow(windowScene: windowScene)
window.rootViewController = UIHostingController(rootView: HomeView())
self.window = window
window.makeKeyAndVisible()
}
}
}
在 AppDelegate class 中,我正在调用 Auth API 并且它 returns 令牌。然后我在 UserDefaults 中设置令牌。 然而此时我的 HomeView 正在打开,它的 .onAppear{} 函数调用另一个 api。我想等待 Auth API 结果的完成。之后要打开 HomeView。我该怎么做?
这也是授权或获取令牌的正确方式吗?
这是我的 AppDelegate、服务、HomeView:
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
Service.shared.authorize(userName: "username", password: "password") { (response) in
UserDefaults.standard.set(response.token, forKey: "Token")
}
return true
}
class Service {
func authorize(userName: String, password: String, completion: @escaping(AuthResponse>) -> ()) {
// Here I'm calling auth api
callApiAsync(urlRequest: urlRequest) { (apiResponse) in
completion(apiResponse)
}
}
}
struct HomeView: View {
@ObservedObject var vm = HomeViewModel()
var body: some View {
Text("HomeView")
.onAppear {
self.vm.getList() { response in
}
}
}
}
我不知道你在哪里初始化你的UIWindow
。 AppDelegate / SceneDelegate
我的 SceneDelegate
样本与 AppDelegate
相似。
请求完成后初始化您的window。
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
Service.shared.authorize(userName: "username", password: "password") { (response) in
UserDefaults.standard.set(response.token, forKey: "Token")
if let windowScene = scene as? UIWindowScene {
let window = UIWindow(windowScene: windowScene)
window.rootViewController = UIHostingController(rootView: HomeView())
self.window = window
window.makeKeyAndVisible()
}
}
}