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

Realm Rx<Single> Operators should close the realm instance to avoid keep multiple global realms open #7581

Closed bobekos closed 2 years ago

bobekos commented 2 years ago

How frequently does the bug occur?

All the time

Description

If we fetch the data over the RxJava function the objects got freezed to make it possible to move them over different threads. That's ok and it should work like this. But what if we doesn't want to listen to the changes but fetch only one result. Then the freezed realm instance should be closed or not? But this seems to not working correctly.

So for example if we have this here (you can do this with all kind of "single" rx-operators like single completable or maybe):

private fun executeSingleRxShot() {
        realm?.let { realm ->
            singleShotDisposable = realm.where(DummyModel::class.java)
                .equalTo("name", "Paul")
                .findAllAsync()
                .asFlowable()
                .observeOn(Schedulers.computation())
                .map { it[0]?.name ?: "no data found" }
                .firstOrError() //convert to single
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe({
                    Log.e("TEST", "JOB DONE BUT GLOBAL INSTANCE IS STILL 2")
                }, {
                    Log.e("TEST", "", it)
                });
        }
    }

the single obsever dispose after the first return but the global instance is still 2. Is that correct? In my option the realm on the other thread should be closed when the single observer get disposed. But this happens only when i close the main realm instance.

So when the "main" realm instance is attached to a livecycle component (for example activity) we have two global instances running which cause the realm file to grow really fast.

I have attached a demo project where you can reproduce this behavior.

Stacktrace & log output

No response

Can you reproduce the bug?

Yes, always

Reproduction Steps

No response

Version

10.8.0

What SDK flavour are you using?

Local Database only

Are you using encryption?

No, not using encryption

Platform OS and version(s)

Android Version 12

Build environment

No response

clementetb commented 2 years ago

@bobekos I am afraid the link to the project has expired, would you mind reposting it again?

bobekos commented 2 years ago

@clementetb Sure have create a repo -> https://github.com/bobekos/RealmRx

clementetb commented 2 years ago

Thanks for reposting the project.

It looks like the garbage collector is not quick in at the right time, and it is causing issues. We would recommend to find an alternative solution to frozen objects. You could use a background handler thread that would leverage the computation. A handler thread would enable notifications on your Realm and will keep the Realm instance on the latest version.

no-response[bot] commented 2 years 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.