JacobSiegel
JacobSiegel

Reputation: 5391

Retrieving specific Firebase data that is stored in a childByAutoId() Reference (Swift)

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" : "[email protected]"
    },
    "XTjy66z8xEWtEiLx6E0mOo1JEaz2" : {
      "email" : "[email protected]"
    },
    "rfzV1saUf3RtPu7YcqJ0VyAOgmd2" : {
      "PlainNote" : {
        "-KIZpfbfHcePU3E8HpeU" : {
          "note" : "Now i only see my own data",
          "title" : "Hello"
        }
      },
      "email" : "[email protected]"
    }
  }
}

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.

Upvotes: 0

Views: 1553

Answers (1)

pho_pho
pho_pho

Reputation: 734

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

Upvotes: 1

Related Questions