0

Here's my code: I've also tried messing with the rules of the data but to no avail. The data keeps getting returned completely randomly, in terms of the "time" values (as well as other values I've looked at).

let path = // Path to messages (/message/<conversationId>/)
let msgRef = // Reference to messages
msgRef.child(id)
    .queryOrdered(byChild: "time")
    .observe(FIRDataEventType.value, with: { (snapshot) in

        let data = JSON(snapshot.value ?? [])
        var messages = [Message]()
        for msg in data {
            let holder = Message(data: msg.1)
            messages.append(holder)
            print("key: \(msg.0) time: \(holder.getTime())")

        }

        // then returns results via completion block

        // The right way:
        let data = snapshot.children
        var messages = [Message]()
        for msg in data.allObjects as! [FIRDataSnapshot] {

            let thisMsgData = JSON(msg.value ?? [])
            let holder = Message(data: thisMsgData)
            messages.append(holder)
            print("key: \(msg.key) time: \(holder.getTime())")

        }

    })
}

Here is how my data is structured:

message: {
    conversationId: {
        msgId: {
            // the rest of the message data
            "time": 20170210051745 <-- as an Int
            // format of date: yyyyMMddHHmmss
        }
    }
}
Kevin R
  • 83
  • 2
  • 9
  • @mrabins: that is not a duplicate. The values stored here are perfectly fine and sortable. The problem is that the block ignores the ordering. – Frank van Puffelen Feb 11 '17 at 15:54
  • Since you're not showing how you actually handling the data in the callback block, I'm going to make an educated guess that you're throwing the ordering away. This is quite common. See my answer here: http://stackoverflow.com/a/40876631/209103 – Frank van Puffelen Feb 11 '17 at 15:56
  • I see, Thank you! For some reason I didn't try .children out. This helped a lot! Again, thank you so much! – Kevin R Feb 13 '17 at 09:07

0 Answers0