2016-07-26 46 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>