Google cloud firestore 将数据从SwiftUI更新到Firestore数据库

Google cloud firestore 将数据从SwiftUI更新到Firestore数据库,google-cloud-firestore,swiftui,avplayer,Google Cloud Firestore,Swiftui,Avplayer,我正在开发一个swiftui应用程序,它可以流式播放电影,并链接到Firebase。目前,我正在尝试获取视频暂停的时间(通过AVPlayer),并将其更新到我的数据库时间字段,以便用户可以从停止的地方开始。我正在尝试使用可编码格式更新数据,并已导入FirebaseFirestoreSwift。但是,代码没有对数据库进行任何更改,因此我想知道是否可以获得帮助。从数据库查询数据的工作非常完美,我只需要关于如何从SwiftUI更新电影集合的不同字段的指导。非常感谢 import FirebaseFir

我正在开发一个swiftui应用程序,它可以流式播放电影,并链接到Firebase。目前,我正在尝试获取视频暂停的时间(通过AVPlayer),并将其更新到我的数据库时间字段,以便用户可以从停止的地方开始。我正在尝试使用可编码格式更新数据,并已导入FirebaseFirestoreSwift。但是,代码没有对数据库进行任何更改,因此我想知道是否可以获得帮助。从数据库查询数据的工作非常完美,我只需要关于如何从SwiftUI更新电影集合的不同字段的指导。非常感谢

import FirebaseFirestoreSwift

...

@State var SectionX: movies

....

//when video is ended record time and update value to firestore time field

.onDisappear {
          avPlayer.pause()
                                    
          let db = Firestore.firestore()
          let currentTime = Float(avPlayer.currentTime().seconds)
                                   
          print(currentTime)

          db.collection("movies").document(SectionX.id)
          SectionX.time = String(currentTime)

          func updateTime(_ mov: movies) {
              do {
                   let _ = try db.collection("movies").document(SectionX.time).setData(from: currentTime)
                 }
              catch {
                       print(error)
                 }
          }
}


struct movies : Codable,Identifiable {
    var id: String
    var title: String
    var img: String
    var video: String
    var description: String
    var genre: String
    var maturity: String
    var time: String
    var year: String
    var acting: String
    var directors: String
    var writers: String
    var producers: String
    var watched: Int
    var rating: Int
}

class getMoviesData : ObservableObject{
    @Published var datas = [movies]()
    
    init(){
        let db = Firestore.firestore()
        db.collection("movies").addSnapshotListener{ (snap, err) in
            if err != nil{
                print((err?.localizedDescription)!)
                return
            }
            
            for i in snap!.documentChanges{
                let id = i.document.get("id") as! String
                let title = i.document.get("title") as! String
                let img = i.document.get("img") as! String
                let video = i.document.get("video") as! String
                let description = i.document.get("description") as! String
                let genre = i.document.get("genre") as! String
                let maturity = i.document.get("maturity") as! String
                let time = i.document.get("time") as! String
                let year = i.document.get("year") as! String
                let acting = i.document.get("acting") as! String
                let directors = i.document.get("directors") as! String
                let writers = i.document.get("writers") as! String
                let producers = i.document.get("producers") as! String
                let watched = i.document.get("watched") as! Int
                let rating = i.document.get("rating") as! Int
                
                self.datas.append(movies(id: id, title: title, img: img, video: video, description: description, genre: genre, maturity: maturity, time: time, year: year, acting: acting, directors: directors, writers: writers, producers: producers, watched: watched, rating: rating))
            }
        }
    }
}


开始工作了!只需要使用updateData方法


let x = db.collection("movies").document(SectionX.id).documentID                                  
db.collection("movies").document(x).updateData(["time":currentTime])