Satish Pandey
Satish Pandey

Reputation: 1184

Firebase realtime database DataSnapshot value to Object conversion

I am trying to convert DataSnapshot value to object. Its working fine, I believe that bean constructor(with arguments) used for conversion. I added some lines of code to perform some opeations, but lines of code I added never get executed. Sample bean class:

@IgnoreExtraProperties
public class DatabaseRecord {

    private String firstName;
    private String lastName;
    private String fullName;

    private DatabaseRecord() {
    }

    public DatabaseRecord(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
        // following code not executing
        this.fullName = firstName + lastName;
    }

    public String getFirstName() { return firstName; }
    public String getLastName() { return lastName; }
    public String getFullName() { return fullName; }
}

Firebase online database flow: enter image description here

Data fetch execution code:

DatabaseReference databaseReference = database.getReference("/user/user1");
databaseReference.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        DatabaseRecord record = dataSnapshot.getValue(DatabaseRecord.class);
        Log.d(TAG, record.getFirstName()+":"+record.getLastName()+":"+record.getFullName());
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
    }
});

Output:

Satish:Pandey:null

  1. Google documentation shows parameterized constructor to set properties, if that is the case why not my code executing?

  2. How to append my code after google conversion finishes in same bean object?

Upvotes: 2

Views: 1156

Answers (2)

leobelizquierdo
leobelizquierdo

Reputation: 1668

In your case the constructor that gets called is the parameterless since you are reading from the database using getValue(), which is required to map the values in the DataSnapshot. According to the doc, these are the rules you must follow to get the DataSnapshot data into a custom Java Class:

  • The class must have a default constructor that takes no arguments

  • The class must define public getters for the properties to be assigned. Properties without a public getter will be set to their default value when an instance is deserialized.

I you want to set the value for the property fullName you can do it in the same getFullName() method. Something like this:

public String getFullName() { 
    if(fullName == null){// it will be null the first time assuming the value doesn't exist in the database.
       fullName = getFirstName() + getLastName();
    }
    return fullName; 
}

Upvotes: 2

Frank van Puffelen
Frank van Puffelen

Reputation: 598728

firebaser here

The Firebase JSON deserializer uses the default (parameterless) constructor and then uses either setter methods or public fields for setting the values.

Most samples (and most real code I write) will have a parameterized constructor for regular creation of the objects, but that parameterized constructor is not used by Firebase.

I would probably make getFullName() a calculated value:

public String getFullName() { return firstName + lastName; }

Upvotes: 2

Related Questions