0

My code is responsible for intercepting the data and sending it to the Realtime Database. The nesting structure is as follows: DeviceID - UserID - SessionID - session parameters - ......

My problem is that when new data is added to the database, they are all sorted differently: DeviceID, UserID, and SessionID in alphabetical order, and the session parameters are sorted by time (that is, the old ones are at the top, the new ones are below).

Can I make it so that when added, the new parameters are at the top of their branch?

Example: There is a device with DeviceID "Samsung".

enter image description here

Further, when adding data, new data will appear at the top or bottom, depending on the alphabet.

enter image description here

But I would like the new data to appear only on top. And so on for each branch.

It's my code:

    class PS() :
    Interceptor {

    @Throws(IOException::class)
    override fun intercept(chain: Interceptor.Chain): Response {
    }

    private fun firebasePush(requestMap: Map<String, Any?>, config: PSConfig): Boolean {
        val db = Firebase.database
        val myRef =
            db.getReference(config.deviceId).
            child(config.userId).
            child(config.sessionId).
            push()
        myRef.setValue(requestMap)
        return true
    }

}

If I didn't understand something, please let me know so I can clarify the question.

Paul
  • 53
  • 3
  • 21

1 Answers1

1

I want to write this in the code so that such an order would be displayed in the Firebase console.

There is no way you can do that. You cannot change in any way the order of the nodes in the Firebase Console. By default, as you already noticed, all the nodes are ordered by key. Keep in mind that all keys in Firebase are strings, and when the strings are ordered, are ordered lexicographically.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • Well thank you. And can I change the most important node (in my case DeviceID)? That is, if information is updated anywhere in the main node, it automatically becomes the first in the list. – Paul Apr 22 '22 at 07:26
  • Yes, you can do that **only** in code. There is no way you can do that in the console. To achieve that in code, you should add a [timestamp](https://stackoverflow.com/questions/43584244/how-to-save-the-current-date-time-when-i-add-new-value-to-firebase-realtime-data) to each object and order the results [descending](https://stackoverflow.com/questions/68594988/sort-data-to-recyclerview-based-on-latest-date-from-firebase/68599883#68599883). – Alex Mamo Apr 22 '22 at 07:29
  • My code is written in kotlin. And as soon as I try to apply the code from these examples, I get an error. Could you help me with my code? – Paul Apr 22 '22 at 07:34
  • "I get an error" doesn't provide enough information so I can help. Without seeing the code that produces that error, it's hard to help. So if you have a hard time implementing some mechanism, please post a new question, here on StackOverflow, using its own [MCVE](https://stackoverflow.com/help/mcve), so I and other Firebase developers can help you. – Alex Mamo Apr 22 '22 at 07:39
  • Can I help you with other information regarding the initial issue that was related to the console? – Alex Mamo Apr 23 '22 at 07:04