Realm 实例已经关闭,无法使用
Realm instance has already been closed, making it unusable
函数fetchData
在片段中。 dataRepository
class 包含我用来从磁盘获取数据的函数:
@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 class:
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();
}
}
}
UserDiskDataclass:
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
returns true 当我关闭应用程序并再次打开它时,为什么会这样,特别是考虑到 realm.getdefaultinstance
就在它之前被调用。
我的同事@zaki50 指出:
DataRepository
保留对 PoliticoDataRepository
的 static 引用。这意味着 DataRepository.getInstance(Realm realm)
可以 return 包含已关闭 Realm 实例的 PoliticoDataRepository
的旧实例。
您必须删除静态字段并确保 DataRepository.getInstance(Realm realm)
始终 return 是引用传递的 Realm 实例的实例。
函数fetchData
在片段中。 dataRepository
class 包含我用来从磁盘获取数据的函数:
@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 class:
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();
}
}
}
UserDiskDataclass:
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
returns true 当我关闭应用程序并再次打开它时,为什么会这样,特别是考虑到 realm.getdefaultinstance
就在它之前被调用。
我的同事@zaki50 指出:
DataRepository
保留对 PoliticoDataRepository
的 static 引用。这意味着 DataRepository.getInstance(Realm realm)
可以 return 包含已关闭 Realm 实例的 PoliticoDataRepository
的旧实例。
您必须删除静态字段并确保 DataRepository.getInstance(Realm realm)
始终 return 是引用传递的 Realm 实例的实例。