0

I created an iOS app that stores the data to Firebase realtime database like this:

users
- ID
-- Email: "test@domain.com"
-- UsersName: "test"
-- Level: 1
-- XP: 0

Now I started with Android development using Kotlin and the data is stored with the first letter of the values name lowercased like this:

users
- ID
-- email: "test@domain.com"
-- usersName: "test" (No mistake, the N is uppercased)
-- level: 1
-- xp: 0 (No mistake, the P is lowercase)

This is my code:

data class User(var UsersName: String = "",
                var XP: Int = 0,
                var Level: Int = 1,
                var Email: String = "")
private fun saveUserToFirebaseDatabase(){
        val uid = FirebaseAuth.getInstance().uid ?: ""
        val ref = FirebaseDatabase.getInstance().getReference("users/$uid")
        val mDatabase = FirebaseDatabase.getInstance().getReference();
        val username = usernameSignUp.text.toString()
        val user = User(username, 0, 1, SignUpEmailtext.text.toString())
        print(user)
        mDatabase.child("users").child(uid).setValue(user)
            .addOnSuccessListener {

            }
            .addOnFailureListener{

            }

    }

I expect the names of the values to be exactly how they are named in the class (like the iOS app saves them).

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
Alex Karapanos
  • 895
  • 2
  • 7
  • 20

1 Answers1

0

Kotlin works on JVM (Java Virtual Machine), so in order to be able to run a .kt file, first of all it should be compiled into a Java file. So when using a class that looks like:

data class User(var UsersName: String = "",
            var XP: Int = 0,
            var Level: Int = 1,
            var Email: String = "")

The corresponding Java file is created and according to the Java Naming Conventions regading variables:

It should be a lowercase letter such as java, lang.

This means that doesn't matter if you choose the name of your fields to start either with an uppercase or a lowercase, all fiels are converted to start with a lowercase. That's why the properties in your database are present like that. Not only in Java/Kotlin is this convention available but also in other programming languages, so I recommend you to use this recommendation in your projects.

Regarding Kotlin, remember that at the end, everything is converted to byte code so it can work on JVM.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • Okay, this means that I have to change the entire iOS app. That's nearly impossible.. My database has a lot of users already.. I wish I new that before I started with iOS version.. Is there any other solution? – Alex Karapanos Mar 29 '19 at 11:16
  • The rules of naming the variables are also specified in the [official documentation](https://kotlinlang.org/docs/reference/coding-conventions.html#naming-rules). I don't know if you can force Kotlin not to do that. I think you should ask a separate question for that. Regarding reading those kind of fields, you can take a look at my answer from this **[post](https://stackoverflow.com/questions/52670456/images-not-loading-from-url-by-picasso-from-firebase-database/52706350#52706350)**. – Alex Mamo Mar 29 '19 at 11:27