Oniikal3
Oniikal3

Reputation: 597

How can I retrieve array data instead of dictionary?

I have messages child that including

- "Some key"
    - "messages"
        - "child by autoid"
            - "text": String
            - "sender": String
        - "child by autoid"
            - "text": String
            - "sender": String
    - "create_date": Date

I need to retrieve messages as Array without auto id. Can I do that? I can't find the way to do it.

Now, I retrieve it to dictionary then map to array. but the problem is index changed while mapping. so how can I retrieve it to array?

Upvotes: 0

Views: 55

Answers (1)

Jen Person
Jen Person

Reputation: 7546

You can observe the "messages" child value, and then create an array of FIRDataSnapshot from the children.

let ref = FIRDatabase.database().reference().child("messages")
ref.observe(.value, with: snapshot in {
    // each snap in snapshots      
    if let snapshots = snapshot.children.allObjects as? [FIRDataSnapshot] {
        for snap in snapshots {
            // do things
        }
    }
})

Upvotes: 1

Related Questions