Swiftui + Firebase - Not able to edit row

爷,独闯天下 提交于 2020-08-10 20:01:06

问题


Currently showing specific routines by authenticated users. I want to be able to edit routines. However, the current app is crashing when I try to edit. Any help would be appreciated! Please let me know if it would be helpful for me to add more information.

My main View:

struct custView: View {
  @ObservedObject var viewModel = RoutinesViewModel()
  var body: some View {
    List {
      ForEach(viewModel.routines, id: \.self) { routine in
        HStack {
          Text(routine.routine)
          NavigationLink(destination: Modify(id: routine.id)) {
            Text("")
          }
        }
      }
    }
  }
}

Code to modify routine:

struct Modify: View {
  @State var txt = ""
  var id = ""

  var body: some View {
    VStack {
      TextField("Edit", text: $txt).textFieldStyle(RoundedBorderTextFieldStyle())
      Button(action: {
        let db = Firestore.firestore().collection("routines")
        db.document(self.id).updateData(["routine": self.txt]) { (err) in
          if err != nil {
            print((err?.localizedDescription)!)
            return
           }
          print("success")
        }
      }) {
        Text("Modify")
      }
    }
  }
}

See below for my database structure.

enter image description here

来源:https://stackoverflow.com/questions/62274356/swiftui-firebase-not-able-to-edit-row

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!