If you add two users, the way you're adding code is going to result in a structure like this:
"googleRef": {
"userID": {
"-Ldfs32189eqdqA1": "userID1",
"-Ldfs32189eqdqA5": "userID2"
},
"gname": {
"-Ldfs32189eqdqA2": "gname1",
"-Ldfs32189eqdqA6": "gname2"
},
"email": {
"-Ldfs32189eqdqA3": "email1",
"-Ldfs32189eqdqA7": "email2"
},
"photoUrl": {
"-Ldfs32189eqdqA4": "photoUrl1",
"-Ldfs32189eqdqA8": "photoUrl2"
}
}
So you have a separate generated push ID (the keys starting with a -
) for each property of each user, which is highly uncommon.
The more idiomatic form of storing user information is either this:
"googleRef": {
"-Ldfs32189eqdqA1": {
"userID": "userID1",
"gname": "gname1",
"email": "email1",
"photoUrl": "photoUrl1"
},
"-Ldfs32189eqdqA5": {
"userID": "userID2",
"gname": "gname2"
"email": "email2"
"photoUrl": "photoUrl2"
},
}
Or (even better) this:
"googleRef": {
"userID1": {
"gname": "gname1",
"email": "email1",
"photoUrl": "photoUrl1"
},
"userID2": {
"gname": "gname2"
"email": "email2"
"photoUrl": "photoUrl2"
},
}
The reasons these last two are more common is that they group the information for each user together, which makes it easier/possible to find information for each user. In both of these cases, you can find users with a specific email address with your query.
The reason the last one is best, is because the information for each user is stored under the user's ID, which is already guaranteed to be unique. This structure makes looking up the user's information by their UID possible without needing a query.
To write a structure like the last example, use:
Map<String, Object> values = new HashMap<>();
values.put("gname", userName)
values.put("email", reEmail)
values.put("photoUrl", userpicUrl)
googleRef.child(userId).setValue(values)
A final note: you can't return whether the node exists or node, since the data is loaded from Firebase asynchronously. To learn more about what that means, and the common workaround (which is to define a callback interface), see getContactsFromFirebase() method return an empty list