ios - 访问 childAutoID 以更新 Firebase 中选定的子值

标签 ios swift firebase firebase-realtime-database

为了填充我的 tableView,我将项目(从结构创建)附加到本地数组:

func loadList() {
        var newAnnotations: [AnnotationListItem] = []
    if let uid = Auth.auth().currentUser?.uid {
        uidRef.child(uid).child("annotations").queryOrderedByKey().observeSingleEvent(of: .value, with: {snapshot in

            for item in snapshot.children {
                let annotationItem = AnnotationListItem(snapshot: item as! DataSnapshot)
                newAnnotations.append(annotationItem)
            }
            annotationList = newAnnotations
            self.tableView.reloadSections([0], with: .fade)
        })
    }
}

当我单击特定行时,我被带到一个 DetailViewController,它只是一个大的 UITextView(命名注释)。显示的 UITextView.text 基于所选的 indexPath.row 并且“notes”值是从数组中检索的。现在用户可以输入一些文本,完成后,将调用 textViewDidEndEditing 函数:

func textViewDidEndEditing(_ textView: UITextView) {
    notes.resignFirstResponder()
    navigationItem.rightBarButtonItem = nil

    let newNotes = self.notes.text
    print(newNotes!)
}

现在我想将 updateChildValues 更改为 newNotes 到我的 JSON 中的子节点“notes”:

"users" : {
  "gI5dKGOX7NZ5UBqeTdtu30Ze9wG3" : {
    "annotations" : {
      "-KuWIRBARv7osWr3XDZz" : {
        "annotationSubtitle" : "1 Cupertino CA",
        "annotationTitle" : "Apple Infinite Loop",
        "notes" : "Does it work?!",
    }

如何访问选定的 autoID 以便更新特定的 notes 节点。到目前为止,我拥有的最好的是:

guard let uid = Auth.auth().currentUser?.uid else { return }
    uidRef.child(uid).child("annotations").(somehow access the specific childID).updateChildValues(["notes": newNotes])

任何帮助将不胜感激。提前致谢

更新

annotationListItem 结构已创建:

struct AnnotationListItem {

let key: String?
var annotationTitle: String?
let annotationSubtitle: String?
let notes: String?
let ref: DatabaseReference?

init(key: String = "", annotationTitle: String, annotationSubtitle: String, notes: String) {
    self.key = key
    self.annotationTitle = annotationTitle
    self.annotationSubtitle = annotationSubtitle
    self.notes = notes
    self.ref = nil
}

init(snapshot: DataSnapshot) {
    key = snapshot.key
    let snapshotValue = snapshot.value as! [String: AnyObject]
    annotationTitle = snapshotValue["annotationTitle"] as? String
    annotationSubtitle = snapshotValue["annotationSubtitle"] as? String
    notes = snapshotValue["notes"] as? String
    ref = snapshot.ref
}

init(Dictionary: [String: AnyObject]) {
    self.key = Dictionary["key"] as? String
    self.annotationTitle = Dictionary["annotationTitle"] as? String
    self.annotationSubtitle = Dictionary["annotationSubtitle"] as? String
    self.notes = Dictionary["notes"] as? String
    self.ref = nil
}

func toAnyObject() -> Any {
    return [
        "annotationTitle": annotationTitle as Any,
        "annotationSubtitle": annotationSubtitle as Any,
        "notes": notes as Any
    ]
}
}

更新

这是 annotationListItem 的创建方式,以便存储在 Firebase 中:

// Using the current user’s data, create a new AnnotationListItem that is not completed by default
    let uid = Auth.auth().currentUser?.uid
    guard let email = Auth.auth().currentUser?.email else { return }
    let title = placemark.name
    let subtitle = annotation.subtitle
    let notes = ""

    // declare variables
    let annotationListItem = AnnotationListItem(
        annotationTitle: title!,
        annotationSubtitle: subtitle!,
        notes: notes)

    // Add the annotation under their UID
    let userAnnotationItemRef = uidRef.child(uid!).child("annotations").childByAutoId()
    userAnnotationItemRef.setValue(annotationListItem.toAnyObject())

最佳答案

我认为你只需要这样做:(因为你已经声明了注释是全局的)

 guard let uid = Auth.auth().currentUser?.uid else { return } 
uidRef.child(uid).child("annotations").(note.key).updateChildValues(["notes": newNotes])

在你更改注释的方法中

关于ios - 访问 childAutoID 以更新 Firebase 中选定的子值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46334595/

相关文章:

angular - firebase.auth.Auth.signInWithCredential 已弃用。请改用 firebase.auth.Auth.signInAndRetrieveDataWithCredential

ios - 如何在不打开 Facebook 应用程序的情况下登录 Facebook?

python - 从云源存储库部署时不会触发 Firestore 云功能

ios - SLComposeViewController 发布图像和 url ios9

javascript - 如何在 iOS Chrome 中使用 CSS scroll snap + picture bug 隐藏滚动条

ios - Swift 如何通过按钮将我的 Tableview Cells accessoryType 重置为 .None

Swift - 使用 URL 下载的低分辨率 firebase 照片

java - 尝试从 Firebase 获取由 UID 和 Push() 方法生成的两个唯一键下的数据并使用查询

ios - 为什么 iPhone XR/Xs Max 不遵守比例限制?

ios - NSArray 和字节顺序