Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Stian Andreassen
Stian Andreassen
523 Points

Firebase - Read from date child? General Discussion Swift

Hi,

I want to access data from a child that is written with a date stamp. This is how I´m writing to Firebase:

//Get user ID

let user = Auth.auth().currentUser!.uid

//Get date

let date = Date()
let calendar = Calendar.current
let hour = calendar.component(.hour, from: date)
let minute = calendar.component(.minute, from: date)
let second = calendar.component(.second, from: date)
let year = calendar.component(.year, from: date)
let month = calendar.component(.month, from: date)
let day = calendar.component(.day, from: date)
let dateAndTime = ("(day)-(month)-(year) at: (hour):(minute):(second)")

//Write to Firebase

let userData = ["Order" : dataArray] as [String : Any]
        self.ref?.child("users").child(user).child("Orders").child(dateAndTime).updateChildValues(userData, withCompletionBlock: { (error, ref) in
            if error != nil{
                print(error!)
                return
            }
        })

I want to read data from the dateAndTime child and post it to a tableView. How can I access the data?

Hope someone can help me out.

1 Answer

Bruce Röttgers
Bruce Röttgers
18,211 Points

Hey,

assuming that you use the realtime database I recommend you checking out the docs on that: https://firebase.google.com/docs/database/ios/read-and-write (It's pretty well documented).