FindFirst returns null

后端 未结 1 1385
悲&欢浪女
悲&欢浪女 2021-01-22 19:54

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

相关标签:
1条回答
  • 2021-01-22 20:43

    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).

    0 讨论(0)
提交回复
热议问题