0

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("$QRCode\uf8ff")
            .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 Answers2

0

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

Muhammad Rio
  • 759
  • 5
  • 6
  • QRcodeSearch.value = profile.toLisst(), won't work as profile is an ArrayList already – Izaac Doyle Apr 07 '22 at 20:14
  • Ive attempted the .notifyObserver but still doesnt work – Izaac Doyle Apr 07 '22 at 20:31
  • "QRcodeSearch.value = profile.toLisst(), won't work as profile is an ArrayList already" It is better to return a `List` rather than `ArrayList` because `ArrayList` is also implements the `List` interface – Muhammad Rio Apr 08 '22 at 05:51
0

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
Papa Yev
  • 618
  • 7
  • 10