2016-01-27 43 views
1

我有一行返回RealmResult與一些排序的數據。如何刪除RealmResults列表中最快和最簡單的所有項目?

我想刪除所有這些項目最快,最簡單。例如:

RealmResults<ElementEntry> currentElements = realm.where(ElementEntry.class).equalTo("type", 1).findAll(); 

//something like this then, would be preffered: 
currentElements.removeFromRealm(); 

但我必須使用迭代器和什麼,而不是,但是當我嘗試,我得到這個錯誤:

java.util.ConcurrentModificationException: No outside changes to a Realm is allowed while iterating a RealmResults. Use iterators methods instead. 

,所以我可以用什麼,如果不是非常迭代器那應該被使用?

回答

3

嘗試清除方法

從文檔:

Removes all objects from the list. This also deletes the objects from the underlying Realm. 
@throws IllegalStateException if the corresponding Realm is closed or in an incorrect thread. 

https://github.com/realm/realm-java/blob/master/realm/realm-library/src/main/java/io/realm/RealmResults.java#L636

realm.beginTransaction(); 
currentElements.clear() 
realm.commitTransaction(); 
+0

這是正確的 - 從這個問題追蹤器也找到了「清除」方法:https://github.com/realm/realm-java/pull/2061 –

+2

Clear()現在折舊了,使用deleteAllFromRealm()代替 – Kenny

2

要啦免費統計在realmResults所有對象做到這一點:

RealmResults<Dog> results = realm.where(Dog.class).findAll(); 

// All changes to data must happen in a transaction 
realm.beginTransaction(); 
results.clear(); 
realm.commitTransaction() 

要遍歷你可以這樣做:

realm.beginTransaction(); 
for (int i = 0; i < results.size(); i++) { 
    results.get(i).setProperty("foo"); 
} 
realm.commitTransaction(); 
+1

再次,clear()現在在RealmResults上折舊使用deleteAllFromRealm() – Kenny

+0

好吧,我在Realm 2.8.3中有以下代碼:'do realm = try Realm() try realm.write { var results = realm.objects(Category.self) results.removeAll() results.deleteAllFromRealm() results.clear() } }趕上{} '全部三種方法沒有在結果<>數據類型中定義。這個問題的*當前*答案是什麼? – axello

2

clear()已被棄用。你應該使用deleteAllFromRealm()

刪除查詢的結果:

final RealmResults<Dog> results = realm.where(Dog.class).findAll(); 

realm.executeTransaction(new Realm.Transaction() { 
    @Override 
    public void execute(Realm realm) { 
     // remove single match 
     results.deleteFirstFromRealm(); 
     results.deleteLastFromRealm(); 

     // remove a single object 
     Dog dog = results.get(5); 
     dog.deleteFromRealm(); 

     // Delete all matches 
     results.deleteAllFromRealm(); 
    } 
}); 

刪除域數據庫中的所有對象:

realm.executeTransaction(new Realm.Transaction() { 
    @Override 
    public void execute(Realm realm) { 
     realm.deleteAll(); 
    } 
}); 
0

好吧,我使用的境界2.8.3,並具有以下代碼:

do { 
     let realm = try Realm() 
     try realm.write { 
      var results = realm.objects(Category.self) 
      results.removeAll() 
      results.deleteAllFromRealm() 
      results.clear() 
     } 
    } catch {} 

結果<>數據類型中未定義所有三種方法。什麼是當前這個問題的答案?

相關問題