skip to Main Content

I have a bottom Sheet Fragment with MVVM which is observing a Firebase Search which is then added to a MutableLiveData.

The .observe(viewLifecycleOwner){it->} is never accessed when data is set from the firebase search even when data is added to the MutableLiveData

private var QrcodeSearch: MutableLiveData<ArrayList<FBAccountNameModel>> = MutableLiveData<ArrayList<FBAccountNameModel>>()
   fun QrCodeScanSearch(QRCode: String) {
        val profile = ArrayList<FBAccountNameModel>()
        db.collection("UserProfiles").orderBy("UserUUID")
            .startAt(QRCode)
            .endAt("$QRCodeuf8ff")
            .limit(5)
            .get()
            .addOnSuccessListener { snapshot ->
                if (snapshot != null) {
                    Log.d("QRSearchProfileAll", "${snapshot.documents}")
                    val document = snapshot.documents
                    document.forEach {
                        val groupUser = it.toObject(FBAccountNameModel::class.java)
                        Log.d("QrUser", groupUser.toString())
                        if (groupUser != null) {
                            Log.d(
                                "QrSearchProfile",
                                groupUser.UserEmail + " " + groupUser.Username + " " + groupUser.UserUUID
                            )
                            profile.add(groupUser)
                        }
                    }
                    QrcodeSearch.value = profile
                }
            }
    }

The query from firebase is being received as the correct data is Logged into logCat

 internal  var qrcodeSearch:MutableLiveData<ArrayList<FBAccountNameModel>>
        get() { return QrcodeSearch}
        set(value) {QrcodeSearch =  value}
groupViewModel.qrcodeSearch.observe(viewLifecycleOwner){it ->
            Log.d("QRCodeSearch Observed Data",it.toString())

        }

The Observation of the data is never accessed and im unsure where to go even when the MuttableLiveData has data set from .value = , I have also tried *.postValue()

2

Answers


  1. The observer will not get notified if you add a list, because LiveData keeps track of each change as a version number simple counter stored as an int.

    Calling setValue() increments this version and updates any observers with the new data only if the observer’s version number is less than the LiveData‘s version,

    the side effect is if the LiveData‘s underlying data structure has changed (such as adding an element to a Collection), nothing will happen to communicate this to the observers.

    My Solution is just simple call .toList()

    // here
    QrcodeSearch.value = profile.toList()
    ...
    

    here is the another answer
    Notify Observer when item is added to List of LiveData

    Login or Signup to reply.
  2. Typically you would use MutableLiveData to modify the data, but use LiveData to observe that data. So can have live data variable pointing to mutable variable.

    private var _liveData: MutableLiveData<ArrayList<FBAccountNameModel>> = MutableLiveData<ArrayList<FBAccountNameModel>>()
    val liveData: LiveData<ArrayList<FBAccountNameModel>> = _liveData
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search