从Firebase获取数据到SwiftUI

时间:2019-12-03 10:43:59

标签: google-cloud-firestore swiftui

我正在学习SwiftUI并使用firebase做一个简单的应用程序。 我的问题是实时获取数据

这是应用程序的工作方式。 我有贷款对象和付款对象。

在第一个视图(列表)中,我实时获取我的所有贷款,可以添加/删除,并且可以使用。 当我单击任何贷款对象时,它将带我到第二个视图(付款),其中我已插入该贷款的所有付款。为了下载我的付款,我需要提供我的loanId,以便Firebase可以过滤我的付款,并仅​​向我发送所选贷款的付款。问题是,当我在过渡期间将我的loanId传递到我的付款视图时,我不知道要这样做,因为下载我的付款的模型是在我进行贷款ID转移之前调用的。

任何建议将不胜感激。

这是我在LoansView中的代码 struct LoansView:查看{

 @State private var showingAddLoan = false
    @ObservedObject var dataListener = LoanListener()

    var body: some View {

        NavigationView {
            List(dataListener.loans) { loan in

                NavigationLink(destination: LoanDetailView(loan: loan)) {
                    ListCell(loan: loan)
                }

            }
            .navigationBarTitle("All Loans")
            .navigationBarItems(trailing:

                Button(action: {
                    self.showingAddLoan.toggle()
                }) {
                    Image(systemName: "plus")
            })
                .sheet(isPresented: self.$showingAddLoan) {
                    AddLoanView()
            }

        }

    }
}

我的loanListener

import Foundation
import Firebase

class LoanListener: ObservableObject {

    @Published var loans = [Loan]()

    init() {
        downloadLoans()
    }

    func downloadLoans() {

        FirebaseReference(.User).document("1234").collection(kLOAN_PATH).addSnapshotListener { (snapshot, error) in

            guard let snapshot = snapshot else {  return }

            if !snapshot.isEmpty {

                self.loans = LoanListener.loanFromDictionaries(snapshot)

            }

        }
    }

    static func loanFromDictionaries(_ snapshot: QuerySnapshot) -> [Loan] {

        var allLoans: [Loan] = []


        for loanData in snapshot.documents {

            allLoans.append(Loan(loanData.data() as NSDictionary))
        }

        return allLoans
    }

}

这是我的付款监听器,但是问题是我必须向其提供loanId才能获得正确的付款

import Foundation
import Firebase

class PaymentListener: ObservableObject {

    @Published var payments = [Payment]()

    init(forLoan: Loan) {
        downloadPayments(forLoan)
    }

    func downloadPayments(_ forLoan: Loan) {

        FirebaseReference(.User).document("1234").collection(kPAYMENT_PATH).whereField(kLOANID, isEqualTo: forLoan.id).addSnapshotListener { (snapshot, error) in

            guard let snapshot = snapshot else {  return }
            print("too")
            if !snapshot.isEmpty {
                print("have")
                self.payments = PaymentListener.paymentFromDictionaries(snapshot)
                print(self.payments.count)
            }

        }
    }

    static func paymentFromDictionaries(_ snapshot: QuerySnapshot) -> [Payment] {

        var allPayments: [Payment] = []


        for paymentData in snapshot.documents {

            allPayments.append(Payment(paymentData.data() as NSDictionary))
        }

        return allPayments
    }

}

这是我的付款视图,我要在其中显示每笔贷款的付款

struct LoanDetailView: View {

    @State var showingAddPayment = false

    var loan: Loan
    @ObservedObject var allPayments = PaymentListener(forLoan: loan)

    var body: some View {

        List(allPayments) { payment in
            PaymentCell(payment: payment)
        }
        .navigationBarTitle("loan.name", displayMode: .inline)
        .navigationBarItems(trailing:

            Button(action: {
                self.showingAddPayment.toggle()
            }) {
                Image(systemName: "plus")
        })
            .sheet(isPresented: self.$showingAddPayment) {
                AddPaymentView(loanId: self.loan.id)
        }

    }
}

但是我得到这个 error

0 个答案:

没有答案