Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ChangeListener is not being called on RealmObject #4984

Closed
Murtowski opened this issue Jul 18, 2017 · 5 comments
Closed

ChangeListener is not being called on RealmObject #4984

Murtowski opened this issue Jul 18, 2017 · 5 comments
Labels
First-Good-Issue First Good Issue - Relatively easy issue for new contributers

Comments

@Murtowski
Copy link

classpath "io.realm:realm-gradle-plugin:3.5.0"

I'm converting objects froming from realm to LiveData (lastly introduced) using extension method

fun<T: RealmObject> T.asLiveData(realm: Realm) = LiveDataRealmObject(this, realm)

Belo is how I convert RealmObject from Realm to set them to LiveData
LiveDataRealmObject::class

class LiveDataRealmObject<T : RealmObject>(private val result: T, realm: Realm) : LiveData<T>() {
    
    private val listener = RealmChangeListener<T> { t ->
        if (t != null && t.isValid)
            realm.executeTransaction {
                value = realm.copyFromRealm(t)
            }

    }

    override fun onActive() {
        result.addChangeListener(listener)
    }

    override fun onInactive() {
        result.removeChangeListener(listener)
    }
}

I retrieve results like this.

fun getCarDetails(): LiveData<CarDetails> {
        return (mRealm.where(CarDetails::class.java).equalTo(CarDetails.ID, 1).findFirstAsync()).asLiveData(mRealm)
    }

And save new one, either override like this

fun saveCarDetails(carDetails: CarDetails){
        mRealm.executeTransactionAsync { realm ->
            carDetails.id = 1 // keep only one copy of the object
            realm?.copyToRealmOrUpdate(carDetails)
// listener isn't trigged
        }
    }

However listener of LiveDataRealmObject is not called - it is called just one after first results came.
This works if I'would operate on RealmResults by quering .findAllAsync()

@Zhuinden
Copy link
Contributor

Zhuinden commented Jul 18, 2017

it is called just one after first results came.

This is expected behavior, see documentation:

Similar to findFirst() but runs asynchronously on a worker thread. An listener should be registered to the returned RealmObject to get the notification when query completes. The registered listener will also be triggered if there are changes made to the queried RealmObject. If the RealmObject is deleted, the listener will be called one last time and then stop. The query will not be re-run.

And if the object did not exist, then the query will return with isValid() == false and will not be re-run.

You can also see #4360 (comment) and #4352 (comment)

If you need to observe whether a RealmObject exists, then you'll need to obtain RealmResults<T> with findAllAsync()

@Murtowski
Copy link
Author

That means copyOrUpdate remove object if it want to updateIt?

@Zhuinden
Copy link
Contributor

Zhuinden commented Jul 18, 2017

No, but it means that if the object didn't exist, then findFirstAsync() won't return it for you in the change listener after it was added.

@Murtowski
Copy link
Author

ok I understand

@dalinaum dalinaum added the First-Good-Issue First Good Issue - Relatively easy issue for new contributers label Jul 18, 2017
@dalinaum
Copy link
Contributor

Hello. @GitHubMurt

As @Zhuinden said, this is expected behavior.

I am closing this issue. If you have another idea, feedback, we can discuss it in another issue. Thanks.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 16, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
First-Good-Issue First Good Issue - Relatively easy issue for new contributers
Projects
None yet
Development

No branches or pull requests

3 participants