观察对象更改时,SwiftUI并未更新视图

时间:2020-09-17 14:31:37

标签: swiftui observedobject zstack

我正在登录并显示数据。用户登录时,数据将正确显示。当我注销并登录另一个帐户时,应该由observedObject更新的数据将调用该函数以重新加载数据。但是,当我登录到应用程序时,主视图仍然显示以前帐户的旧数据。我不知道是什么问题。我什至尝试注销时清除了课堂上的数据,也尝试了多种方法来解决问题,但是视图仍然没有更新。寻求帮助,tq。

struct ContentView: View {
    @EnvironmentObject var authService:AuthService
    var body: some View{
        ZStack{
            if(!authService.signedIn){
                RegisterView()
            }
            else{
                HomePageView()
            }
        }
    }}
struct HomePageView: View {
    @ObservedObject var shopList:ShopJSON = .shared

    var body: some View {
        
        TabView{
            HomePromotionView(shopList: shopList)
                .tabItem {
                        Image(systemName: "house")
                }.tag(1)

            NavigationView{
                BookingView()
            }
                .tabItem {
                    Image(systemName: "calendar")
                }.tag(3)
        }
        .onAppear(perform: ShopJSON.shared.reload) //I try to force reload data.
    }
}
struct HomePromotionView: View {
    @State private var selectedSegment = 0
    @ObservedObject var shopList : ShopJSON

    private let homeSegment = ["PROMOTION", "HISTORY", "MESSAGE"]
    
    var body: some View {
        NavigationView {
            
            VStack {
                ScrollView(.horizontal, showsIndicators: false){
                    
                    HStack{
                        
                        ForEach(shopList.shops){ item in
                            MerchantBar(name:item.name!, icon: item.icon!, id: item.shopID!)
                        }
class ShopJSON: ObservableObject {
    
    @Published var shops = [Shops]()
    @Published var bcsts = [Bcast]()
    @Published var selectedID : Int?
    static let shared = ShopJSON()

    let auth = UserDefaults.standard.string(forKey: "Auth")
    
    private init() {
        load()
    }
    func reload() {
        load()
    }
    func clear() {
        self.shops = [Shops]()
        self.bcsts = [Bcast]()
        self.selectedID = nil
    }
    func load() {
        
        let url = URL(string: "https://12345/shop/")!
        var request = URLRequest(url: url)
        request.setValue("application/json", forHTTPHeaderField: "Content-Type")
        request.setValue(self.auth, forHTTPHeaderField: "token")
        request.httpMethod = "GET"
        
        URLSession.shared.dataTask(with: request) { data, response, error in
            guard let data = data else {
                print("No data in response: \(error?.localizedDescription ?? "Unknown error").")
                return
            }
            if let decodedShops = try? JSONDecoder().decode(Json4Swift_Base.self, from: data) {
                DispatchQueue.main.async {
                    self.shops = decodedShops.shops!
                    self.bcsts = decodedShops.bcast!

退出按钮

            Text("Sign Out")
                .foregroundColor(Color.gray)
                .onTapGesture {
                    self.authService.signOut()
                    UserDefaults.standard.set(nil, forKey: "Auth")
                    //self.shopList.clear()       //force clean data but view didn't update while login with another account
            }

2 个答案:

答案 0 :(得分:0)

我已经观察到相同的问题,并通过使用EnvironmentObjects而不是ObservedObjects解决了该问题。就数据封装而言,这并不理想,但对我有用。

答案 1 :(得分:0)

问题是我在类中声明了auth令牌。用户注销时,类内的令牌没有更新。仍然会使用旧令牌来获取结果。

解决方案只是在函数内部获取令牌,而不是在类中声明

解决方案:

func load() {
        let url = URL(string: "https://12345/shop/")!
        var request = URLRequest(url: url)
        request.setValue("application/json", forHTTPHeaderField: "Content-Type")
        let auth = UserDefaults.standard.string(forKey: "Auth")
        request.setValue(auth, forHTTPHeaderField: "token")
        request.httpMethod = "GET"