Naz
Naz

Reputation: 149

Android Realm - RealmObject returning after deletion

I'm running in to an issue where I delete a RealmObject (and confirm its gone using StethoRealm), but when I create and save a new RealmObject with the same variable value, the deleted RealmObject reappears along with my new RealmObject.

With the below object - I delete the myObject1 by matching the 'id', but when I create a new MyObject myObject2 with the same 'name' both objects appear in my realm

Any tips on why this might be happening?

Example:

public class MyObject extends RealmObject {

@PrimaryKey
private String id;

private String name;

// getters and setters

}

Edit

Delete call

@Override
public void deleteMyObjectById(final String id, final OnDeleteMyObjectById callback) {
    Realm realm = new MyRealmConfiguration().getDefaultRealm();

    final RealmResults<MyObject> myObjects = realm.where(MyObject.class).equalTo(RealmTable.MyObject.ID, id).findAll();

    realm.executeTransaction(new Realm.Transaction() {
        @Override
        public void execute(Realm realm) {
            myObjects.deleteAllFromRealm();

            if (callback != null) {
                callback.onSuccess();
            }
        }
    });
    realm.close();
}

Add call

@Override
public void addMyObject(MyObject myObject, OnSaveMyObjectCallback callback) {
    Realm realm = new MyRealmConfiguration().getDefaultRealm();
    realm.executeTransaction(new Realm.Transaction() {
        @Override
        public void execute(Realm realm) {
            realm.copyToRealmOrUpdate(myObject);

            if (callback != null) {
                callback.onSuccess();
            }

        }
    });
    realm.close();
}

Upvotes: 0

Views: 568

Answers (2)

Im Batman
Im Batman

Reputation: 1876

Try something like this. get the instance inside transcation method and delete. i have been using this for one of my app. and it works fine with deletion process

 Realm realm = Realm.getDefaultInstance();
        realm.executeTransaction(new Realm.Transaction() {
            @Override
            public void execute(Realm realm) {
                RealmResults<MyObject> myObjects = realm.where(MyObject.class).equalTo(RealmTable.MyObject.ID, id).findAll();
                myObjects.deleteAllFromRealm();
            }
        });

Upvotes: 1

darkterbears
darkterbears

Reputation: 169

I'm not sure as to how you are deleting your objects. The way I'm deleting them (and it works) is...

final RealmResults<UserCredentials> credentialResults = realm.where(UserCredentials.class).findAll();

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

realm.close();

Of course, you are using MyObject instead of UserCredentials, and you aren't deleting everything of that type, but it should be similar enough. Try using executeTransaction and realm.close() if you aren't already.

Upvotes: 1

Related Questions