Martin819
Martin819

Reputation: 535

Object isn't inserted into Room database

I have an Android app which uses Room Database. I have several tables and all of them works just fine, except one - the User object(table). I'm using the same approach for all of the object(tables), but still, data isn't inserted into this exact one. The table is still empty. Even though other tables are populated just fine. Bellow is my code.

In the Fragment, if the user presses the button, the fabClickHandler method is called. The method checks if there is already a user in DB created. If it is, it returns his API key and uses for a request. If it is not present, it asks for an input of API key. Then it should store it to DB, but it doesn't. It asks again next time and I have checked the table and it is indeed empty.

If populated the table with the User object manually, everything works just fine.

User.java:

@Entity
public class User {

    @PrimaryKey
    private int uid;
    private String name;
    private String apikey;

    public User(int uid, String name, String apikey) {
        this.uid = uid;
        this.name = name;
        this.apikey = apikey;
    }

    @Ignore
    public User(String name, String apikey) {
        this.name = name;
        this.apikey = apikey;
    }

    @Ignore
    public User() {
    }

    public int getUid() {
        return uid;
    }

    public void setUid(int uid) {
        this.uid = uid;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getApikey() {
        return apikey;
    }

    public void setApikey(String apikey) {
        this.apikey = apikey;
    }
}

UserDao.java:

@Dao
public interface UserDao {

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    public void insertUser(User user);

Fragment:

public void fabClickHandler() {
        database = AppDatabase.getInstance(activity.getApplicationContext());
        userDao = database.userDao();
        String keyFromDB = "NOTHING_FETCHED";
        try {
            keyFromDB = checkDBForUserAPIKey()[0];
            Log.d(DEBUG_TAG_INFO, "First keyFromDB: " + keyFromDB);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        if (!keyFromDB.equals("GOT_NULL") && !keyFromDB.equals("NOTHING_FETCHED") && !keyFromDB.equals("NOTHING_FOUND")) {
            Log.d(DEBUG_TAG_INFO, "Second keyFromDB: " + keyFromDB);
            resourcesController.getFactoriesList(keyFromDB, factoriesAdapter);
        } else {
            pricesController.getMarketPricesAvgsList(pricesAdapter);
            final EditText input = new EditText(activity);
            final String[] user_apikey = {"NONE_ENTERED"};
            AlertDialog.Builder builder = new AlertDialog.Builder(activity);
            builder.setMessage(R.string.dialog_apikey_enter_message).setTitle(R.string.dialog_apikey_enter_title).setView(input);
            builder.setPositiveButton(R.string.dialog_apikey_enter_positive_button, (dialog, which) -> {
                user_apikey[0] = input.getText().toString();
                Log.d(DEBUG_TAG_INFO, "Third keyFromDB: " + user_apikey[0]);
                new Thread(() -> {
                    userDao.insertUser(new User(1, "Main", user_apikey[0]));
                });
                Toast.makeText(activity, "Syncing...", Toast.LENGTH_SHORT).show();
                resourcesController.getFactoriesList(user_apikey[0], factoriesAdapter);
            });
            builder.setNegativeButton(R.string.dialog_apikey_enter_negative_button, (dialog, which) -> {
                user_apikey[0] = "DIALOG_CANCELED";
                Log.e(DEBUG_TAG_ERROR, "API key dialog canceled");
                Toast.makeText(activity, "Dialog canceled", Toast.LENGTH_LONG).show();
            });
            AlertDialog apikey_dialog = builder.create();
            apikey_dialog.show();
        }
    }

when I print the "Third keyFromDB: " (see in the code), the API key is there, but then the object is not inserted into a table.

I don't know what else I should do. I have used this same DB approach previously and never had a problem.

Upvotes: 0

Views: 290

Answers (1)

Santhosh Joseph
Santhosh Joseph

Reputation: 934

Why you are using these lines in your User Entity?

@Ignore
public User(String name, String apikey) {
    this.name = name;
    this.apikey = apikey;
}

@Ignore
public User() {
}

@Ignore ignores the marked element from Room's processing logic.

This annotation can be used in multiple places where Room processor runs. For instance, you can add it to a field of an Entity and Room will not persist that field.

Remove it from your User entity, and it will works.

For reference Defining data using Room entities

UPDATE:

Is this the only line where you inserting a new user? userDao.insertUser(new User(1, "Main", user_apikey[0]));

If yes, then it will return only one user, because all the time you are inserting 1 as uid and it will replace the previous user as the uid is the primary key.

Hope this helps you.

Upvotes: 1

Related Questions