2016-07-26 92 views
1
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; 
} 

我查询后境界使用commitTransaction()两次的方法,并得到这个错误,我每查询后试图realm.beginTransaction()realm.commitTransaction()java.lang.IllegalStateException:不允许嵌套事务。每次的BeginTransaction()

还有一件事:这个查询只从realm db读取数据。

Ror在数据库中写入数据我们通常使用commit来保存数据。

我试图realm.commitTransaction()还,但我得到同样的错误。

+0

我对Realm一无所知。在读取查询之前真的需要调用beginTransaction()吗? –

+0

是的,如果你没有调用,那么应用程序崩溃,错误是“你需要调用reaml.beginTransactio()” – user3449611

+0

你有没有尝试在循环内添加'beginTransaction'和'commitTransaction'? –

回答

1

我有同样的问题更新和循环与一些RealmObjects阵列。

我的条件之前与交易

if(realm.isInTransaction()){ 
    realm.commitTransaction(); 
} 

希望这有助于增加这一点。

+0

感谢您的帮助 我已经把我的境界查询行,你给定的条件 如果(realm.isInTransaction()){ realm.beginTransaction(); RealmResults categories = realm.where(CategoriesDto.class)。equalTo(“parentId”,categoriesDto.getCategoryId())。findAll();对于(int i = 0; i user3449611

+0

你的条件必须在我的答案中。错误是说你有一个开放的领域交易。我的情况关闭了待处理的交易。然后你打开一个新的。 –

+0

问题是我试图通过将realmObjects添加到listOfCategories来设置我从查询获得的realmList,但是领域说,如果它在传递 以及通过使用关闭该事务之后也不能使用该引用你的情况,我得到了同样的错误。 所以你必须使用“copyFromRealm” – user3449611

2

最后得到的答案有很多的后尝试

当我通过应用for循环 代替,加入境界对象我用了一个境界方法copyFromRealm(), 下面是一段代码

listOfCategories.addAll(realm.copyFromRealm(categories)); 

这个我想是这样,没有必要申请提交并开始交易,我们只是执行读取操作。

+0

接受你自己的答案,所以它可以帮助其他用户,并保持未答复的队列更清晰。 –

1

如果持续时间你把所有的类别DTOS:

public class CategoriesDto extends RealmObject { 
    @PrimaryKey 
    private String categoryId; 

    @Index 
    private String identifier; 

    @Index 
    private String parentId; 

    private CategoriesDto parent; 

    //getters, setters 
} 

然后你就

realm.executeTransaction(new Realm.Transaction() { 
    @Override 
    public void execute(Realm realm) { 
     realm.insertOrUpdate(dtos); 
     RealmResults<CategoriesDto> children = realm.where(CategoriesDto.class) 
                .isNotNull("parentId") 
                .isNull("parent") 
                .findAll(); 
     for(CategoriesDto category : children) { 
      CategoriesDto parent = realm.where(CategoriesDto.class).equalTo("categoryId", category.getParentId()).findFirst(); 
      category.setParent(category); 
     } 
     realm.insertOrUpdate(children); 
    } 
}); 

然后,你可以做

public RealmResults<CategoriesDto> getListOfCategories(String type) { 
    return realm.where(CategoriesDto.class) 
       .equalTo("identifier", type) 
       .equalTo("parent.categoryId", categoriesDto.getCategoryId()) 
       .findAll(); 
} 

但在你的情况,你只是不得不打开交易,并使用copyFromRealm()。在这种情况下,我不确定你为什么使用RealmList<T>