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

deleteAllFromRealm() issue with gradle-plugin:10.13.0 - OK with realm-gradle-plugin:10.12.0 #7748

Closed LeonardDaVinci closed 1 year ago

LeonardDaVinci commented 1 year ago

How frequently does the bug occur?

All the time

Description

Hello,

I wanted to upgrade realm in my Android (Dolphin) project from dependencies = classpath 'io.realm:realm-gradle-plugin:10.12.0' to dependencies = classpath 'io.realm:realm-gradle-plugin:10.13.0'

I have noticed that using exactly the same code, deleteAllFromRealm() does NOT work with gradle-plugin:10.13.0 - records are not deleted at all!

Making the same with with gradle-plugin:10.12.0 all records get deleted as usual.

This is the piece of code, that has two differents results using it with Realm 10.12.0 or Realm 10.13.0

public void deleteOrderHistoriesFromDB(final String time) {
    realm.executeTransactionAsync(new Realm.Transaction() {
        @Override
        public void execute(@NonNull Realm realm) {
            RealmResults<OderHistoryOl> results = realm.where(OderHistoryOl.class).equalTo("time", time).findAll();
            results.deleteAllFromRealm();
        }
    });
}

Am I making some mistake in code?

Thank you for help.

Stacktrace & log output

No response

Can you reproduce the bug?

Yes, always

Reproduction Steps

Leaving exactly the same java code, but only changing build.gradle dependencies = classpath 'io.realm:realm-gradle-plugin:10.12.0'
to dependencies = classpath 'io.realm:realm-gradle-plugin:10.13.0' will allways cause the issue

Version

classpath 'io.realm:realm-gradle-plugin:10.13.0'

What SDK flavour are you using?

Local Database only

Are you using encryption?

Yes, using encryption

Platform OS and version(s)

Android 7, Android 11

Build environment

Win10 64Bit Android Studio Dolphin | 2021.3.1
Android Gradle Plugin Version 7.3.0
Gradle version 7.6

cmelchior commented 1 year ago

Hi @LeonardDaVinci

I tried to reproduce this behavior, but it seems to work also in 10.13. Your code looks correct, but it sounds like something you can reproduce?

Perhaps it is a problem with your input parameter to the function. Have you verified that RealmResults<OderHistoryOl> results actually returns any results?

LeonardDaVinci commented 1 year ago

Thank you. Yes, I checked again with debug and have seen that input parameters (time) is always present, having right values with 10.12 and also with 10.13 Really strange.. 10.12 delete but 10.13 not..

What I suppose: my app is a waiter order app for restaurant. In this case it does

  1. Build and array on new products (order)
  2. Save to Realm (asynchronously) as backup before calling an api to save to web server
  3. Save to pc using api / apache web server
  4. If api saving = Success then
  5. I make results.deleteAllFromRealm() to delete backup order from Realm on device

And here is the problem: could it be that the asynchronous saving at step 2 is very very slow and it is not immediately saved on Realm but they get saved AFTER my step 5?

Is there a way to immediately force a flush when saving or deleting objects in Realm?

edualonso commented 1 year ago

Hi @LeonardDaVinci. It seems the asynchronous call may be running too slowly. You could try to run those steps synchronously and see if the problem is solved. You can try with realm.executeTransaction instead, that would run it synchronously and you might avoid a possible race condition.

LeonardDaVinci commented 1 year ago

Hi @edualonso. I made test, writing also using .allowQueriesOnUiThread(true) but still no luck. I wrote .executeTransaction but app crash!... I have to think some other solution..

clementetb commented 1 year ago

Hi @LeonardDaVinci we need more context about the issue to tell what's going on. Would you be able to create and share a test project that reproduces this issue?

github-actions[bot] commented 1 year ago

This issue has been automatically closed because there has been no response to our request for more information from the original author. With only the information that is currently in the issue, we don't have enough information to take action. Please reach out if you have or find the answers we need so that we can investigate further.