简体   繁体   中英

StateObject does not change on Appear but changes value on Button Tap

On Clicking the Button present in BillContributorView.swift, the view updates but not on Appear. I have tried swapping StateObject with ObservedObject but not only does it not help but also, I read that we should instantiate our ObservableObject with StateObject.

BillContributorView.swift

 @StateObject var billManager = BillManager()
var body: some View {
        VStack {
            HStack {

            Text("Get: \(String(format: "%.2f", billManager.toGetAmount))")
                .font(.footnote)
            
            Button {
                billManager.updateToGet(contributorID: memberID)
            } label: { Text("To Get") } 
           
        }
        .onAppear(perform: {
            billManager.updateRoomID(name: roomID)
            billManager.updateToGet(contributorID: memberID )
        })

    }

BillManager

class BillManager: ObservableObject {

    @Published var roomID: String

    @Published var toGetbills = [Bill]()
    @Published var toGetAmount: Double = 0

    
    init(name: String? = nil) {
        if let name = name {
            roomID = name
        } else {
            roomID = ""
        }
    }
    
    func updateRoomID(name: String) {
        roomID = name
    }
 
    func updateToGet(contributorID: String) {
        let collectionName = "\(roomID)_BILLS"
        toGetAmount = 0
        
        db.collection(collectionName)
            .whereField("payer", isEqualTo: Auth.auth().currentUser?.uid ?? "")
            .whereField("contributor", isEqualTo: contributorID)
            .addSnapshotListener { snapshot, error in
               
                DispatchQueue.main.async {
                    
                    guard let doc = snapshot?.documents else {
                        print("No Doc Found")
                        return
                    }
                    
                    self.toGetbills = doc.compactMap { queryDocumentSnapshot -> Bill? in
                        let result = Result { try queryDocumentSnapshot.data(as: Bill.self) }
                        
                        switch result {
                        case .success(let bill):
                            return bill

                        }
                    }
                }
            }
        
        toGetAmount = 0
        for bill in toGetbills {
            toGetAmount += bill.itemPrice
        }
    }

Fixed the issue by updating the value inside the closure.

db.collection(collectionName)
            .whereField("payer", isEqualTo: Auth.auth().currentUser?.uid ?? "")
            .whereField("contributor", isEqualTo: contributorID)
            .addSnapshotListener { snapshot, error in
                
                self.toGetAmount = 0
                    
                    guard let doc = snapshot?.documents else {
                        print("No Doc Found")
                        return
                    }
                    
                DispatchQueue.main.async {
                    self.toGetbills = doc.compactMap { queryDocumentSnapshot -> Bill? in
                        let result = Result { try queryDocumentSnapshot.data(as: Bill.self) }
                        
                        switch result {
                        case .success(let bill):
                            self.toGetAmount += bill.itemPrice
                            return bill
                            
                        case .failure( _):
                            print("Failure To Get Bill Request")
                            return nil
                        }
                        
                    }
                }
            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM