samedi 25 avril 2020

reasing variable to new query in Realm

I'm new to Realm and I coulnd't find anything related to this in the documentation. I'm trying to make a method which gets a specific object from the Realm database by ID when I call it. It looks like this:

 private var singleVendor: SingleVendor? = null

     fun subscribeToVendor(
        liveData: MutableLiveData<SingleVendor>,
        id: Int
    ) {
        singleVendor?.removeAllChangeListeners()
        val singleVendor = getRealm().where(SingleVendor::class.java).equalTo("id", id).findFirstAsync()
        singleVendor?.addChangeListener<SingleVendor> { _ ->
            liveData.value = singleVendor
        }    
    }

It is needed for a VendorFragment, when someone clicks on a vendor in a list, I'd like to get the specific vendor data and update the VendorFragment with it. But when I try call this method again, singleVendor gets invalidated. I'm using LiveData here, because I'd like to encapsulate Realm.

Am I missing something? Is there a pattern for this? Thank you in advance!

Aucun commentaire:

Enregistrer un commentaire