Home » Android » Crash without log: toObject

Crash without log: toObject

Posted by: admin November 1, 2017 Leave a comment

Questions:

I am trying to map the document to a custom class but the app constantly crashes without logs.

I am still learning, so the map only contains 1 entry, called "pin" with value "0".

This is the code:

public Response<User> login(LoginRequest request) {
    Task<DocumentSnapshot> task = firestore.collection(NetDatabase.Collections.USERS)
            .document(request.getDni())
            .get();

    DocumentSnapshot document = null;
    try { 
        document = Tasks.await(task); 
    } catch (Exception e) {
        return new Response<>( new Error()); 
    }

    if (document != null && document.exists()){
        User user = document.toObject(User.class); // <-- CRASH
        if (user.getPin().equals(request.getPin())){
            return new Response<>(user);
        }
    }
    return new Response<>(new Error());
}

And this is the User class:

public class User extends RealmObject implements NetResponse {

    private String id;
    private String pin;

    public User(){ }

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

    public String getId() {
        return id;
    }

    @Override
    public void setId(String id) {
        this.id = id;
    }

    public String getPin() {
        return pin;
    }
}

public interface NetResponse {
    void setId(String id);
}

I tried to swap between private and public but the result remains the same. The document is retrieved correctly, it contains a single entry with values mentioned above.

Answers:

I’ve solved it.

Basically, since Firestore is creating the Object, making it extends RealmObject may generate some kind of conflict. After erasing it, it all works as intended.