Realm实例已经关闭,使其无法使用

时间:2017-02-05 21:33:09

标签: java android realm observable rx-java2

函数fetchData位于片段中。 dataRepository类包含用于从磁盘获取数据的函数:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    final View view = inflater.inflate(R.layout.fragment_user, container, false);

    realm = Realm.getDefaultInstance();

    Log.i("realm", "oncreateView");

    items = new ArrayList<>();

    try {
        Log.i("realm", "check" + (realm == null));
    } catch (Exception e) {
        e.printStackTrace();
    }

    fetchData();

    return view;
}

@Override
public void onDestroy() {
    super.onDestroy();
    if (realm != null && !realm.isClosed())
        realm.close();
    Log.i("realm", "ondestroy");
}

    private void fetchData() {

    abcDataRepository = DataRepository.getInstance(realm);

    Observable<List<User>> observable = bcDataRepository.getUser_List();

    observable.subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(Users-> {
                Log.i("data", Users.size() + "");

                for (int i = 0; i < Users.size(); i++) {
                    items.add(Users.get(i));
                }
            });
}

DataRepository类:

public class DataRepository {

    private Realm realm;

private static PoliticoDataRepository dataRepository = null;

private PoliticoDataRepository(Realm realm) {
    this.realm = realm;
}

public static PoliticoDataRepository getInstance(Realm realm) {
    if (dataRepository == null)
        dataRepository = new PoliticoDataRepository(realm);

    return dataRepository;
}

@Override
public Observable<List<User>> getUser_List() {

    final UserDiskData diskData = new UserDiskData(realm);

    if (diskData.User_Size() > 0) {
        return diskData.getUser_List();
    } else {
        final UserCloudData cloudData = new UserCloudData(realm);
        return cloudData.getUser_List();
    }
}
}

UserDiskData类:

class  UserDiskData {

private Realm realm;

UserDiskData(Realm realm) {
    this.realm = realm;
}

int User_Size() {
    final RealmResults<User> results = realm.where(User.class).findAll();
    Log.i("disk", "pulledData" + results.size());
    return results.size();
}

@Override
public Observable<List<User>> getUser_List() {

    Log.i("disk", "called");

    final RealmResults<User> results = realm.where(User.class).findAll();
    ArrayList<User> list = new ArrayList<>();
    for (int i = 0; i < results.size(); i++)
        list.add(results.get(i));

    return Observable.just(list);
}

}

应用程序第一次运行正常,但是当我退出应用程序并再次打开它时,它会崩溃并发出错误This Realm instance has already been closed,使其无法使用。 如果我在最后跳过realm.close,它的工作完全正常。我应该手动关闭领域对象,如果是这样如何? 出于某种原因,当我关闭应用并再次打开它时,realm==null会返回true,为什么会这样做,特别是考虑到realm.getdefaultinstance之前正在调用它。

1 个答案:

答案 0 :(得分:1)

我的同事@ zaki50指出:

DataRepository保留对PoliticoDataRepository静态引用。这意味着DataRepository.getInstance(Realm realm)可以返回包含已关闭的Realm实例的PoliticoDataRepository的旧实例。

您必须删除静态字段并确保DataRepository.getInstance(Realm realm)始终返回引用传递的Realm实例的实例。