realm / realm-java

Realm is a mobile database: a replacement for SQLite & ORMs
http://realm.io
Apache License 2.0
11.45k stars 1.75k forks source link

Deleted objects are still visible on other threads #7766

Closed dakexuan closed 1 year ago

dakexuan commented 1 year ago

How frequently does the bug occur?

Always

Description

I successfully delete a piece of data in the worker thread, and then it can be queried in the UI thread or another worker thread. For each operation I get an instance "Realm.getInstance(mRealmConfiguration);",and close immediately after use.Am I using it the wrong way?Interestingly, if I delete the data and do not query it, but close the App, open the App again and query it, I will not find the deleted data

Stacktrace & log output

No response

Can you reproduce the bug?

Always

Reproduction Steps

No response

Version

10.8.0

What Atlas App Services are you using?

-- select --

Are you using encryption?

-- select --

Platform OS and version(s)

android

Build environment

Android Studio version: ... Android Build Tools version: ... Gradle version: ...

cmelchior commented 1 year ago

Realm is a MVCC database, so it can handle multiple versions of data at the same time.

Depending on exactly how you delete the data and how you check it on another thread, it is possible that the other thread is still seeing the older data. You can always force an update by calling realm.refresh(), but that is not really recommended.

If you could show the actual code showing this behaviour, we could probably tell you why it isn't working as expected.

edualonso commented 1 year ago

I can't see anything fundamentally wrong in the code you wrote so it seems like there may be a race condition elsewhere. If you could issue a sample project containing a minimal reproduction case we could help you further.

dakexuan commented 1 year ago

Thank you very much. I found the wrong reason.The deleted data exists in another relationship

dakexuan commented 1 year ago

@edualonso How can I accurately delete a piece of data?This data exists in multiple relationships

public class User extends RealmObject {
    @PrimaryKey
    long id;
    String name
    Dog dog;

    //getters setters etc.
}

public class Dog extends RealmObject {
    @PrimaryKey
    int id;
    String name;

    //getters setters etc.
}
Realm realm = RealmHelper.instance().realm();
Dog dog= realm.where(Dog.class).equalTo("id", id).findFirst();
        if (dog!= null) {
            realm.beginTransaction();
            dog.deleteFromRealm();
            realm.commitTransaction();
        }
realm.close();

If dog exists in User,I can also find it。thx