Home » Android » Realm not fully saved

Realm not fully saved

Posted by: admin November 1, 2017 Leave a comment

Questions:

is realm have something like listener for commiting data?
my code is

...  
RealmDatabase.submitNewUser(mainActivity.getMyRealm(), userModel);
mainActivity.closeKeyboard();
mainActivity.onBackPressed();
...


public static void submitNewUser(Realm myRealm, UserModel user) {
    myRealm.beginTransaction();
    UserDb userDb = myRealm.createObject(UserDb.class);
    userDb.setUserid(getNextUserId(myRealm));
    userDb.setName(user.name);
    ....
    myRealm.commitTransaction();
}

private static int getNextUserId(Realm myRealm) {
    try {
        return myRealm.where(UserDb.class).max("userid").intValue() + 1;
    } catch (ArrayIndexOutOfBoundsException e) {
        return 1;
    }
}

after save data, i closed fragment and back to previous fragment.
on start function, checking if have data

@Override
public void onStart() {
    super.onStart();
    if (loading.isShowing()) loading.dismiss();
    if (reloadList && checkContent()) {
        ....
    }
    reloadList = true;
}

private boolean checkContent() {
    users = RealmDatabase.loadUserList(mainActivity.getMyRealm());
    if (users.size() > 0 && users.get(0).userid > 0) {
        // note:
        // user id is auto increament while saved
        // if no data, return dataset with userid = 0 for other purpose
        return true;
    } else {
        ....
        return false;
    }
}

public static List<UserModel> loadUserList(Realm myRealm) {
    List<UserModel> list = new ArrayList<>();

    RealmResults<UserDb> results = myRealm.where(UserDb.class).findAll();
    results = results.sort("userid", Sort.DESCENDING);

    for (UserDb result : results) {
        UserModel userModel = new UserModel();
        userModel.userid = result.getUserid();
        ....
        userModel.note = result.getNote();
        list.add(userModel);
    }
    if (list.size() == 0) {
        UserModel userModel = new UserModel();
        userModel.userid = 0;
        userModel.note = "You still have no user at this time";
        list.add(userModel);
    }
    return list;
}

checkContent(), user.size detected as 1 (new data is added) but userid still 0.
am i miss something in this logic? because everything is working well if i reopen app after add new user.

update
after using listener, i got my dataset but still not showing my content. after some trial i found that my list view is not showing the data even after i re-input data and do notifydataset on adapter.

users = RealmDatabase.loadUserList(mainActivity.getMyRealm());
homeAdapter.reloadList(users);

....

public void reloadList(List<UserModel> users) {
    this.users = users;
    notifyDataSetChanged();
}

update 2
everything going well for the 2nd, 3rd, and later item except the first one

Answers:

is realm have something like listener for commiting data?

Yes

realmResults.addChangeListener(realmChangeListener);

One must keep a field reference to the RealmResults.

everything is working well if i reopen app after add new user.

Probably the ArrayList you build from the RealmResults is not updated.