首页 > 解决方案 > Reload data but the ObservableObject is not working

问题描述

With SwiftUI, I'm trying to update the data when I create a Favorite object in the database. It is created successfully, and when I call the functions reload, it can get the correct information.

However, when I change the View, it is not reloaded in the View.

MY DATA CODE:

// Favorite
struct  Favorite: Decodable, Identifiable {
    var id:Int?
    public var id: Int?
    public var name: String?
    public var type: String?
    var addres: String?
}


class Favorite: ObservableObject {
    var userID = userViews().id
    @Published var favorites = [Favorite]()
    
    init(){
        load()
    }
    
    func load() {
        let url = URL(string: "https://***********/getFavoriteApi/")

        URLSession.shared.dataTask(with: url) {(data, response, error) in
            do {
                if let d = data {
                    let decodedLists = try JSONDecoder().decode([Favorite].self, from: d)
                    DispatchQueue.main.async {
                        self.favorites= decodedLists
                        print(self.favorites)
                    }
                }else {
                    print("No Data")
                }
            } catch {
                print ("Error")
            }
            
        }.resume()
         
    }
}

If I print(self.favorites) it can see the new Optional after create Favorite, but view is not reload it.

MY VIEW CODE:

struct FavoriteView: View {
    @ObservedObject var favorites = Favorite()
    
    var name = UserDefaults.standard.value(forKey: "name")as? String ?? "null"
    var userID = userViews().id
    
    
    var body: some View {
        VStack{
            Text("My Favorite")
                .font(.title)
            GeometryReader{mainView in
                ScrollView(.vertical, showsIndicators: false) {
                    
                    VStack{
                        
                        ForEach(favorites.posts){post in
                            
                            NavigationLink(destination: FavoriteDetail(post: post)) {
                                
                                GeometryReader{item in
                                    
                                    rentView1(post: post)
                                        // scaling effect from bottom....
                                        .scaleEffect(scaleValue(mainFrame: mainView.frame(in: .global).minY, minY: item.frame(in: .global).minY),anchor: .bottom)
                                        // adding opacity effect...
                                        .opacity(Double(scaleValue(mainFrame: mainView.frame(in: .global).minY, minY: item.frame(in: .global).minY)))
                                }
                                .frame(height: 100)
                                
                            }                    
                        }
                    }
                    .padding(.horizontal)
                    .padding(.top,25)
                    
                }
                .zIndex(1)
                
            }
        }
    }
}

I'm afraid I'm doing something wrong or maybe I don't get how the ObservedObjects work with JSONDecoder.

Any help or explanation is welcome! Thank you.

标签: iosswiftui

解决方案


我在ForEach里面使用时遇到了同样的问题ScrollView

检查这个讨论它可以帮助你找到一个黑客


推荐阅读