Antonio Costa
Antonio Costa

Reputation: 313

Save userId on model Firebase

I have a question related with how to save the user information with firebase. I extended the user authentication and created a new node on the json tree with users, each user has his own id generated by firebase and the user info is inside that key/id. The thing is, each time I do this:

myRef.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                for (DataSnapshot snapshot : dataSnapshot.getChildren()) {

                    User user = snapshot.getValue(User.class);

                    if (number.equals(String.valueOf(user.getPhone()))) {
                        Log.d("here", "i entered");
                        key = snapshot.getKey();
                        userFriend = user;
                    }


                }

I map the user info inside that key inside my user model that I have in my code, but in my project next steps I need to have the key for the specific user too. And here I am just mapping the user info without the key. Is there a way I can add a string id inside my model and automatically add the key to that id field?

userModel

package com.esmad.pdm.friendlymanager.model;

import java.util.ArrayList;


public class User {

    private String id;
    private String username;
    private int age = -1;
    private String phone;
    private int gamesPlayed = 0;
    private ArrayList<User> FriendList = new ArrayList<User>();

    // CONSTRUCTOR
    public User() {

    }

    public User(String username) {
        this.username = username;
    }

    // GETTERS & SETTERS
    public String getUsername() { return username; }
    public void setUsername(String username) { this.username = username; }

    public User(String id, String username, int age, String phone, int gamesPlayed, ArrayList<User> users) {
        this.username = username;
        this.age = age;
        this.phone = phone;
        this.gamesPlayed = gamesPlayed;
        this.FriendList = users;
    }



    public int getAge() { return age; }
    public void setAge(int age) { this.age = age; }

    public String getPhone() { return phone; }
    public void setPhone(String phone) { this.phone = phone; }

    public int getGamesPlayed() { return gamesPlayed; }
    public void setGamesPlayed(int gamesPlayed) { this.gamesPlayed = gamesPlayed; }

    public ArrayList<User> getUsers() { return FriendList; }
    public void setUsers(ArrayList<User> users) { this.FriendList = users; }
    }

Upvotes: 2

Views: 739

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 600016

There is no way to get the key of the snapshot automatically injected when you call snapshot.getValue(User.class).

But you can easily add an extra call that adds the key to the User object. You'll first need to add a getter and setter for id to your User class:

@Exclude
public String getId() { return id; }
@Exclude
public void setId(String id) { this.id = id; }

As you probably already noticed I annotated these with @Exclude. This tells the database client to ignore the properties when reading from/writing to the database. Without the annotation, you'd also get an id property in each User's node in the database.

Now you can simply set and get the key when you're reading the property values:

public void onDataChange(DataSnapshot dataSnapshot) {
    for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
        User user = snapshot.getValue(User.class);
        user.setId(snapshot.getKey());
        System.out.println(user.getId());
    }
}

In the above snippet snapshot.getValue(User.class) gets a user object with all the regular properties, and then user.setId(snapshot.getKey()) adds the key to that object.

When writing back to the database you could also use user.getId() to determine where to write:

ref.child(user.getId()).setValue(user);

Upvotes: 6

Related Questions