0

I'm new to Firebase and have a function that writes all of my event ID's to an array. I want to use the last value in that array (the last event ID) to lookup the children of that specific eventID.

I know how to get the last item in the array but how do I put that into my .child() path?

I tried the code below, but it doesn't seem to work. I'm guessing that because .child("(lastEvent)") isn't a valid path.

let lastEvent = eventIDArray.last
refHandle = ref.child("Bouts").child("\(lastEvent)")

How do I plug the lastEvent value in as my path? Or is that even possible? Again, total newbie- alternatives welcome.

mugx
  • 9,869
  • 3
  • 43
  • 55
Katie
  • 21
  • 7

1 Answers1

0

Sorting and filtering data

you can use sorting and filtering function to get the item.
To get the last item you can write the query like this.**

let recentBoutsQuery = (ref?.child("Bouts").queryLimited(toLast: 1))!

This will return 1 entry from last of your database which the last entry.

You can learn more from the firebase documentation. Work with Lists of Data

Community
  • 1
  • 1
  • Thanks Ashutosh, how do I take a snapshot of that value, do I add the .observe onto the end of it? I need to grab the children of that node. – Katie Jan 08 '18 at 06:52
  • Yes,you can loop the result. since it has only one item loop will run only once. recentBoutsQuery.observe(.value) { snapshot in for child in snapshot.children { } } – Ashutosh Barthwal Jan 08 '18 at 07:02