FindFirst returns null

廉价感情. 提交于 2019-12-02 05:43:38

问题


I'm using Realm to provide the database for my application. But...

After login, the server returns the data and I create the account (of AccountManager) and save these datas at the database of the application, like this (at an AsyncTask, of course):

UserRealm userRealm = new UserRealm();
//setter of the userRealm...

Realm realm = Realm.getInstance(context);
realm.beginTransaction();
realm.copyToRealmOrUpdate(userRealm);
realm.commitTransaction();
realm.close();

After, I close the LoginActivity and at the onResume of the MainActivity, I try to load the user, like this(at an AsyncTask, again...):

public static UserRealm getUser(Context context) {
  try {
    return Realm.getInstance(context).where(UserRealm.class).findFirst();
  } catch (Exception e) {
    if(DebugUtil.DEBUG) { //enabled
      e.printStackTrace();
    }
  }
  return null;
}

But this returns null, I don't know what happens with it.

UserRealm: https://gist.github.com/ppamorim/88f2553a6ff990876bc6


回答1:


AsyncTask is in a threadpool, and considering you open Realm instances that you never close with your getUser() call, your Realm version becomes locked at the version when you first called getUser().

return Realm.getInstance(context).where(UserRealm.class).findFirst(); // never closed

So even though you commit a transaction on another thread in the threadpool, not all threads will be up to date (because you locked them on an old version by opening Realm instances that are never closed), and sometimes the object will be null.

Solution, close all Realm instances on background threads (or force an update if that's not enough for some reason).



来源:https://stackoverflow.com/questions/32107880/findfirst-returns-null

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!