public RealmList<CategoriesDto> getListOfCategories(String type){
final RealmList<CategoriesDto> listOfCategories = new RealmList<>();
final CategoriesDto categoriesDto = realm.where(CategoriesDto.class).equalTo("identifier", type).findFirst();
if (categoriesDto != null) {
realm.beginTransaction();
RealmResults<CategoriesDto> categories = realm.where(CategoriesDto.class).equalTo("parentId", categoriesDto.getCategoryId()).findAll();
for (int i = 0; i < categories.size(); i++) {
listOfCategories.add(categories.get(i));
}
// realm.commitTransaction();
}
return listOfCategories;
}
我在一个方法中查询 Realm 两次,并收到此错误,我在每次和之后尝试了 realm.beginTransaction()
和 realm.commitTransaction()
每个查询。
还有一件事:此查询仅从 Realm 数据库读取数据。
在数据库中写入数据时,我们通常使用提交来持久化数据。
我也尝试过 realm.commitTransaction()
但我遇到了同样的错误。
最佳答案
我在使用一些 RealmObject 更新和循环数组时遇到了同样的问题。
我在交易条件之前添加了此内容
if(realm.isInTransaction()){
realm.commitTransaction();
}
希望这有帮助。
关于android - java.lang.IllegalStateException : Nested transactions are not allowed. 在每个 beginTransaction() 之后使用 commitTransaction(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38593913/