[ad_1]
Problem: When writing a new user into the firebase database from a released version, instead of the given node names, the application somehow saves them as “a,b,c,d… etc”. The code works fine in local builds (see results below in the screenshots). The values are inserted correctly as well as the number of properties given.
Thanks for any help you can offer!
minSdk 24
targetSdk 32
Code used to persist the data:
The user class
//DTO
class UserData() {
var id: String = "";
var avatar: String = "";
var bio: String = "";
var city: String = "";
var color: String = "";
var firstName: String = "";
var lastName: String = "";
var hobbies: ArrayList<String> = ArrayList();
var age: Long = 0;
var onboardingPhase: OnboardingPhase = OnboardingPhase.changePassword;
fun hashmapToValue(map: HashMap<String, String>) {
id = map["id"]!!;
avatar = map["avatar"]!!;
bio = map["bio"]!!;
city = map["city"]!!;
color = map["color"]!!;
firstName = map["firstName"]!!;
lastName = map["lastName"]!!;
age = map["age"]!! as Long;
onboardingPhase = OnboardingPhase.valueOf(map["onboardingPhase"]!!);
}
}
Function to persist the user
fun syncUserData(callback: () -> Unit = {}) {
userRef = FirebaseDatabase.getInstance().getReference("users/${auth.uid}")
userRef.get()
.addOnSuccessListener { task ->
try {
val dataSnapshot = task.value as HashMap<String, String>
user = UserData();
user.hobbies = dataSnapshot["hobbies"] as ArrayList<String>;
user.hashmapToValue(dataSnapshot);
} catch (e: NullPointerException) {
//if there is no entry found in the database, create a new one
//This is where the user is persisted to the database.
val userData = UserData();
userData.hobbies = arrayListOf("")
userData.id = auth.uid!!;
val jsonString: String = Gson().toJson(userData) //set to json string;
val jsonMap: Map<String, Any> = Gson().fromJson(
jsonString,
object : TypeToken<HashMap<String?, Any?>?>() {}.type
)
userRef.updateChildren(jsonMap);
}
callback();
}
}
(Also tried it with out the json map)
val userData = UserData();
userData.id = auth.uid!!;
userData.hobbies = arrayListOf("")
userRef.setValue(userData);
The correct node names
When saving from local build.
The messed up node names
When saving from a play store version.
[ad_2]