0

尽管问题很简单,但我还没有找到满意的答案。我想根据 Firebase 中的值更新切换开关。我已经添加了侦听器,但在将 Bool 转换为 Binding Bool 时遇到问题,感谢您的帮助。

struct oneSeqeuncer : Identifiable{
    var id: String
    var status: Bool
}


struct Sequencers: View {
@ObservedObject var seqModel = SequencerModel()
@State private var novaseq404A :Bool = true
@State private var novaseq404B :Bool = true
@State private var novaseq297A :Bool = true
@State private var novaseq297B :Bool = true
var body: some View {
    ZStack{
        VStack{
            Text("Sequencers")
                .foregroundColor(.white)
                .font(.title)
                .fontWeight(.bold)
                .padding()
            List{
                HStack{
                    Text("404")
                        .font(.title)
                        .padding()
                    Toggle("", isOn: $novaseq404A)
                        .onChange(of: novaseq404A) { newValue in
                            updateStatus(name: "404A", status: novaseq404A)
                        }
                    Toggle("", isOn: $novaseq404B)
                        .padding()
                        .onChange(of: novaseq404B) { newValue in
                            updateStatus(name: "404B", status: novaseq404B)
                        }
                }
                HStack{
                    Text("297")
                        .font(.title)
                        .padding()
                    Toggle("", isOn: $novaseq297A)
                        .onChange(of: novaseq297A) { newValue in
                            updateStatus(name: "297A", status: novaseq297A)
                        }
                    Toggle("", isOn: $novaseq297B)
                        .padding()
                        .onChange(of: novaseq297B) { newValue in
                            updateStatus(name: "297B", status: novaseq297B)
                        }
                }
            }
        }
    }.onAppear(){
        self.seqModel.fetchData()
        for seq in seqModel.seqs{
            if seq.id == "404A"{
                novaseq404A = seq.status
            }
            if seq.id == "404B"{
                novaseq404A = seq.status
            }
            if seq.id == "297A"{
                novaseq297A = seq.status
            }
            if seq.id == "297B"{
                novaseq297B = seq.status
            }
 }
    func updateStatus(name: String, status: Bool){
    let timeInterval = NSDate().timeIntervalSince1970
    let myInt = Int(timeInterval)
    let db = Firestore.firestore()
    if status == false{
        db.collection("Sequencers").document(name).updateData([
            "status": false,
            "lastChange" : myInt
        ]){ error in
            if error != nil{
                print(error!)
            }
        }
    }
    else{
        let docRef  = db.collection("Sequencers").document(name)
        
        docRef.getDocument {(document, error) in
            if error != nil {
                print(error!)
            }
            if let document = document, document.exists{
                let data = document.data()
                if let lastChange = data!["lastChange"]! as? Int{
                    let timeOff = myInt - lastChange
                    if let timeOffTotal = data!["timeOff"]! as? Int{
                        let newTimeOff = timeOffTotal + timeOff
                        db.collection("Sequencers").document(name).updateData([
                            "timeOff" : newTimeOff
                        ])
                    }
                }
                db.collection("Sequencers").document(name).updateData([
                    "previousChange": data!["lastChange"]!,
                    "status": true ,
                    "lastChange" : myInt
                    ])
            }
        }
    }
}
}

struct Sequencers_Previews: PreviewProvider {
    static var previews: some View {
        Sequencers()
    }
}

下面是我存储“序列器”的模型

import Foundation
import FirebaseFirestore
import Firebase


class SequencerModel : ObservableObject {
    @Published var seqs = [oneSeqeuncer]()
    private var db = Firestore.firestore()

    func fetchData(){
        db.collection("Sequencers").addSnapshotListener { (querySnapshot, error) in
          guard let documents = querySnapshot?.documents else {
            print("No documents")
            return
          }

          self.seqs = documents.map { queryDocumentSnapshot -> oneSeqeuncer in
          
              let data = queryDocumentSnapshot.data()
              let id = queryDocumentSnapshot.documentID
              let status = data["status"] as? Bool
              print(id)
              print(status as Any)

              return oneSeqeuncer(id: id, status: status!)
          }
        }
    }
}
4

1 回答 1

1

我的解决方案并不理想,但解决了,我意识到.fetchData()我调用的函数响应时间太长。理想情况下,我应该使用一些完成处理程序......但是我只是将我TabView的 on更改ContentView为首先显示另一个页面,以便有时间完成对 Firebase 的调用,这允许我的 for 循环.onAppear具有非空的可迭代对象。再次不确定这是否真的属于“答案”,但只是想分享我的临时解决方案作为一个选项。

于 2022-02-03T02:53:11.597 回答