ios 第二次导航至屏幕时,未显示SwiftUI警报

r7s23pms  于 2023-02-01  发布在  iOS
关注(0)|答案(1)|浏览(150)

我是swiftUI新手。我有一个应用程序正在进行API调用,在此应用程序中,如果API不返回响应,我希望显示错误。当我第一次导航到屏幕时,它显示警报消息,但当我返回到上一屏幕并再次导航回同一屏幕时,警报未显示。
结构 Jmeter 板视图:查看{

@ObservedObject var viewModel = DashboardViewModel()
@State private var showingAlert = true

init() {
    UINavigationBar.appearance().titleTextAttributes = [.foregroundColor: UIColor.black]
    self.showingAlert = true
}

var body: some View {
        ZStack {
            Color.white
            if case .LOADING = viewModel.currentState {

                loaderView()
                .onAppear(perform: viewModel.getDashboardData)
            } else if case .SUCCESS(let dashboard) = viewModel.currentState {
                ScrollView(.vertical, showsIndicators: false) {
                    VStack(alignment: .leading, spacing: 10) {
                        HStack {
                            Text("Date:")
                                .foregroundColor(Color.black)
                            Text(dashboard.date)
                                .foregroundColor(Color.black)
                        }
                        VStack(alignment: .leading) {
                            Text("HDURL:")
                                .foregroundColor(Color.black)
                            Text(dashboard.hdurl)
                                .foregroundColor(Color.black)
                        }

                        HStack {
                            Text("Media Type:")
                                .foregroundColor(Color.black)
                            Text(dashboard.mediaType)
                                .foregroundColor(Color.black)
                        }
                        HStack {
                            Text("Service Version:")
                                .foregroundColor(Color.black)
                            Text(dashboard.serviceVersion)
                                .foregroundColor(Color.black)
                        }
                        VStack(alignment: .leading) {
                            Text("Title:")
                                .foregroundColor(Color.black)
                            Text(dashboard.title)
                                .foregroundColor(Color.black)
                        }
                        VStack(alignment: .leading) {
                            Text("url:")
                                .foregroundColor(Color.black)
                            Text(dashboard.url)
                                .foregroundColor(Color.black)
                        }
                        VStack(alignment: .leading) {
                            Text("Explanation:")
                                .foregroundColor(Color.black)
                            Text(dashboard.explanation)
                                .foregroundColor(Color.black)
                        }
                    }
                    .padding(.horizontal, 10)
                    .padding(.top, UIApplication.shared.keyWindow!.safeAreaInsets.top )

                }
                .padding(.top, UIApplication.shared.keyWindow!.safeAreaInsets.top )
            } else if case .FAILURE(let error) = viewModel.currentState {
                VStack {
                    Text("No Data")
                        .foregroundColor(Color.black)
                }

          
            }

        }
         .navigationBarTitle("Dashboard", displayMode: .inline)
         .alert(item: $viewModel.appError) { appAlert in
             Alert(title: Text("Error"),
                   message: Text("""
                     \(appAlert.errorString)
                     Please try again later!
                     """
                     )
             )
         }

         .ignoresSafeArea(.all)
}

}
枚举视图状态 Jmeter 板{案例开始案例加载案例成功( Jmeter 板型号: Jmeter 板)用例失败(错误:字符串)}
协议从服务器获取 Jmeter 板数据{func getDashboardData()var当前状态:视图状态 Jmeter 板{获取集}}
类 Jmeter 板视图模型:可观察对象,从服务器获取 Jmeter 板数据{

struct AppError: Identifiable {
    let id = UUID().uuidString
    let errorString: String
}

@Published var appError: AppError?

let monitor = NWPathMonitor()
let monitorPostUser = NWPathMonitor()

let queue = DispatchQueue(label: "InternetConnectionMonitor")

var cancelable: Set<AnyCancellable> = []
@Published var currentState: ViewStateDashboard = .START

init() {
    self.currentState = .LOADING
}

// GET Method
func getDashboardData() {
    print("fetch dashboard data")
    self.currentState = .LOADING
    monitor.pathUpdateHandler = { pathUpdateHandler in
        if pathUpdateHandler.status == .satisfied {

            APIClient.dispatch(
                APIRouter.GetDashboardData(queryParams:
                APIParameters.GetDasbhboardParams(apikey: "API_KEY")))
            .sink { completion in
                switch completion {
                case .finished:
                    print("Execution Finihsed dashboard.")
                case .failure(let error):
                    DispatchQueue.main.async {
                        print("dashboard error", error)
                        self.appError = AppError(errorString: error.localizedDescription)
                        self.currentState = .FAILURE(error: error.localizedDescription)
                    }
                }
            }
        receiveValue: {  dashboardData in
            print("received dashboard data", dashboardData)
            self.currentState = .SUCCESS(dashboardModel: dashboardData)
        }.store(in: &self.cancelable)

        } else {
            DispatchQueue.main.async {
                self.currentState = .FAILURE(error: StringConstants.NoInterNet)
                self.appError = AppError(errorString: StringConstants.NoInterNet)
                print("no internet get users")
            }
        }
    }
    monitor.start(queue: queue)

}

}
有什么问题吗?任何帮助都将不胜感激。

xriantvc

xriantvc1#

关闭警报时,viewModel.appError将设置为nil。如果希望它再次出现,必须再次将其设置为某个值。

相关问题