This Realm instance has already been closed, making it unusable + RxJava

后端 未结 2 803
自闭症患者
自闭症患者 2021-01-01 02:45

First of all i manage realm instance by Repository class:

public class RealmRepository {

private Lock lock;

protected RealmRepository() {
    lock = new Re         


        
相关标签:
2条回答
  • 2021-01-01 03:11

    The finally method will be executed before the calling method gets the result, in this case.

    0 讨论(0)
  • 2021-01-01 03:21

    Realm instances are reference counted after the initial initialization call. Each call to close() decrements this reference count, and each call to getDefaultInstance() will increase this reference count. Realm's resources are freed once the reference count reaches 0.

    Considering the fact that you're using a Lock to block accesses to your Realm instance, you're causing reference count to reach 0 when you make your close() call, then not reinitializing the Realm configuration after Realm has already freed its resources.

    In order to fix this, you need your calls to getDefaultInstance() to overlap before the subsequent call to close() to ensure that your reference count remains > 0 while you're still actively using Realm, otherwise you need to reinitialize the entire Realm configuration each time which will come with a performance impact.

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