JEGADEESAN S
JEGADEESAN S

Reputation: 618

How to delete realm list in Realm Database

I'm having a Realm class called Sale. I had a list of objects called allSales for Sale class. Now I want to delete some objects in Sale Realm class.

    RealmResults<Sale> allSales = realm.where(Sale.class).findAll();
    RealmList<Sale> toBeDeleted = new RealmList<Sale>();

    for(Sale sale : allSales){
        String salesDate = sale.getSaleDate();
        if(salesDate.equals("01-01-2017")) {
            toBeDeleted.add(realm.copyToRealm(sale));
        }
    }

    realm.beginTransaction();
    toBeDeleted.clear();
    realm.commitTransaction();

The data was not cleared in Sale class instead toBeDeleted list only cleared.

Upvotes: 1

Views: 4579

Answers (2)

Lalchand
Lalchand

Reputation: 115

You have to call this method from realm transaction...

realm.executeTransaction(new Realm.Transaction() {
                                    @Override
                                    public void execute(Realm realm) {
                                        saleRalmList.deleteLastFromRealm();// use to delete all

                                        //**OR** use in for loop to delete perticulr record as a location
                                        saleRalmList.deleteFromRealm(location);

                                    }
                                });

Upvotes: 0

M-Wajeeh
M-Wajeeh

Reputation: 17284

You can use RealmList.deleteFromRealm()/RealmList.deleteAllFromRealm() to remove items from both the list and Realm. See the methods in API docs here: https://realm.io/docs/java/latest/api/io/realm/RealmList.html

Upvotes: 3

Related Questions