且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

检索存储在childByAutoId()参考(Swift)中的特定Firebase数据

更新时间:2022-11-22 18:13:13

不知道如果你有答案,但在这里,以防万一(我有同样的问题)。我理解的方式是,您要获取每次生成的唯一密钥,以便可以访问存储在单个密钥下的数据。您可以生成一个字符串数组,然后使用它来提取单个索引处的键值,然后将其反馈到您的数据检索代码中。这是这样做的:
$ b $ pre $ usersRef.child(usersId!)。child(PlainNote)。observeEventType(.ChildAdded, withBlock:{snapshot in

var idKeys = [String]()//这将保存您的键

用于捕捉snapshot.children.allObjects {

让id = snap as!FIRDataSnapshot
$ b idKeys.append(String(id.key))

}}

$ p

现在你有了你的密钥,你可以使用var selectedKey = idKeys [x]来选择一个。


This problem may be difficult to explain.

Here is the JSON that is generated by firebase:

{
  "users" : {
    "2W1Sse5kiZarFQ5k8UKjP87D8Rk2" : {
      "PlainNote" : {
        "-KIZYVAGZQczTSI1L4Qi" : {
          "note" : "Finally works",
          "title" : "Whats up"
        },
        "-KIZY_M3FMW0m8mgx4Am" : {
          "note" : "Aye",
          "title" : "Db"
        },
        "-KIZpAEsa7-wSCOJfHvv" : {
          "note" : "This wont work for some reason",
          "title" : "Helloo"
        }
      },
      "email" : "jacobsiegel@gmail.com"
    },
    "XTjy66z8xEWtEiLx6E0mOo1JEaz2" : {
      "email" : "123@gmail.com"
    },
    "rfzV1saUf3RtPu7YcqJ0VyAOgmd2" : {
      "PlainNote" : {
        "-KIZpfbfHcePU3E8HpeU" : {
          "note" : "Now i only see my own data",
          "title" : "Hello"
        }
      },
      "email" : "whatsup@gmail.com"
    }
  }
}

Here is how I generate each plain note in swift:

let title = plainNoteTitleTextField.text!
    let note = bodyOfNoteTextView.text!
    let usersId = FIRAuth.auth()?.currentUser?.uid

        if title != "" || note != "" {
            let data = [
                "title": title,
                "note": note
            ]

self.usersRef.child(usersId!).child("PlainNote").childByAutoId().setValue(data)

After this, I populate the tableview in realtime with the firebase realtime database:

override func viewDidAppear(animated: Bool) {
    var localNotes = [String]()
    var localBody = [String]()
    let usersId = FIRAuth.auth()?.currentUser?.uid
    usersRef.child(usersId!).child("PlainNote").observeEventType(.ChildAdded, withBlock: { snapshot in
        let title = snapshot.value?.objectForKey("title") as! String
        var x = snapshot.value?.objectForKey("note") as! String

        if x.characters.count > 20 {
            x = x[0...20] + "..."
        } else {
            x = x + "..."
        }

        localNotes.insert(title, atIndex: 0)
        localBody.insert(x, atIndex: 0)
        self.notes = localNotes
        self.noteBody = localBody
        self.tableView.reloadData()
    })
}

My problem: I am trying to access the PlainNote references so that when a tableview cell is tapped, I can pull the specific note title and body from firebase. I am not sure how I would do that without looking at my database every time, because firebase creates a unique reference each time a new note is created.

(P.S.: If you think you understand, but need more details, don't hesitate to ask!) Thank you.

not sure if you have the answer yet, but here it is just in case (I had the same problem). The way that I understand it is that you want to obtain the unique keys that are generated each time so that you can access the data stored underneath a single one. You could generate a string array and then use this to extract the value of a key at a single index which can then be fed back into your data retrieval code. This is done like so:

usersRef.child(usersId!).child("PlainNote").observeEventType(.ChildAdded, withBlock: { snapshot in

var idKeys = [String]()  //this will hold your keys

for snap in snapshot.children.allObjects {

let id = snap as! FIRDataSnapshot

idKeys.append(String(id.key))

}}

Now you have your keys, you can select one using var selectedKey = idKeys[x].

Hope this helps