Search code examples
androidrealm

Realm not fully saved


Is realm have something like listener for committing 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 saving the 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 increment 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 missing something in this logic? Because everything is working well if I reopen app after I add a 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 is going well for the 2nd, 3rd, and later item except the first one


Solution

  • as suggested by @epicPandaForce answer, i use listener to my code.
    and to solved my problem as i mentioned in the last comment in @epicPandaForce answer, i change my code like this

    getMyRealm().addChangeListener(new RealmChangeListener<Realm>() {
         @Override
         public void onChange(Realm element) {
               // previous code in my activity
               // getFragmentManager().popBackStackImmediate();
    
               // new code in my activity
               fragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
               FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
               fragmentTransaction.replace(R.id.container, fragment);
         }
     });  
    

    those code not really inside the listener instead calling function on my activity. those placement just for where its called.