簡體   English   中英

將不同的依賴 LiveData 對象合並為一個

[英]Merging different depending LiveData objects into one

這是我的問題。 我必須將來自三個不同LiveData對象的數據生成到一個對象中。 其中兩個依賴於其中之一。 只有在接收到所有三個對象數據時才應觸發觀察者。

下面是一個例子:

結果對象包含產品的信息和該產品的特定評論評級。 結果對象如下所示:

public class RatedProduct {
    private String productKey; // database key to the product
    private String ratingKey; // database key to the specific rating
    private Product product; // the product information
    private Rating rating; // the specifiv rating information
    private HashMap<String, Boolean> customTags; // list of tags
...
}

源分布在不同的數據庫位置。

第一個對象UserRating包含附加數據 ( customTags ) 和只有兩個鍵,引用產品和評級信息。

public class UserRating {
    private String ratingKey;
    private String productKey;
    private HashMap<String, Boolean> customTags;
...
}

包含特定信息的RatingProduct對象。

public class Rating {
    private float value; // value of the rating
    private String productKey; // database key to the product
    private String ratingComment;
...
}
public class Product {
    private String name; // name of the product
    private HashMap<String, Boolean> ratings; // list of all ratings
...
}

任務是將UserRatingRatingProduct所有數據收集到一個RatedProduct但隱藏必要的步驟。

我認為最好的方法是使用三個不同的LiveData對象並在MediatorLiveData的幫助下將它們合並

但我不確定如何以正確的方式做到這一點。 我有點卡住了。 我知道如何添加UserRatingLiveData源到MediatorLiveData但我丟了怎么添加剩下的兩個,叫他們RatingLiveDataProductLiveData相對於包含在按鍵UserRatingLiveData

第二個問題是, MediatorLiveData<RatedProduct>應該只在檢索到所有數據時更新,而不是只有其中一個數據時才更新。

這就是我所擁有的(或多或少什么都沒有)。 有趣的部分是LiveData<RatedProduct> getRatedProductLiveData()我猜:

public class UserViewModel extends ViewModel {
    ...
    private UserRatingsLiveData mUserRatingsLiveDate;

    public UserViewModel() {

        //
        // Getting current user
        FirebaseUser mUser = FirebaseAuth.getInstance().getCurrentUser();

        if (mUser != null) {
            this.mUserRatingsLiveDate =
                    new UserRatingsLiveData(
                            mUsersDatabaseReference.child(mUser.getUid() + "/" + DB_PATH_RATINGS));
        } else {
            Log.d(LOG_TAG, "mUser is NULL");
        }
    }

    @NonNull
    public LiveData<RatedProduct> getRatedProductLiveData() {
        final MediatorLiveData<RatedProduct>  liveDataMerger = new MediatorLiveData<> ();
        final UserRating receivedUserRating = new UserRating();


        liveDataMerger.addSource(this.mUserRatingsLiveDate, new Observer<UserRating>() {
            public void onChanged(UserRating userRatings) {

            }
        });
        return liveDataMerger;
    }
}

作為附加提示:數據庫是 Firebase RealTimeDatabase,如果這有幫助的話。

像這樣嘗試:


class MyViewModel {
    val mainLiveData: LiveData<CustomClass> get() = _mainLiveData

    private val firstLiveData = MutableLiveData<String>()
    private val secondLiveData : MutableLiveData<Int>? = null
    private val thirdLiveData : MutableLiveData<Long>? = null

    private val _mainLiveData = MediatorLiveData<CustomClass>().also { mergeLiveData ->
        mergeLiveData.addSource(firstLiveData) {
           if (secondLiveData != null ) mergeLiveData.removeSource(secondLiveData)
           if (thirdLiveData != null) mergeLiveData.removeSource(thirdLiveData)

           secondLiveData = getSecondLiveData(firstLiveData.value)
           thirdLiveData = getThirdLiveData(firstLiveData.value)

           mergeLiveData.addSource(secondLiveData) {
               updateMainLiveData(mergeLiveData)
           }
           
           mergeLiveData.addSource(thirdLiveData) {
               updateMainLiveData(mergeLiveData)
           }
        }
    }

    private fun updateMainLiveData(mainLiveData: MediatorLiveData<CustomClass>) {
        val first = firstLiveData.value ?: return
        val second = secondLiveData.value ?: return
        val third = thirdLiveData.value ?: return

        mainLiveData.value = CustomClass(first, second, third)
    }

    data class CustomClass(
        val first: String,
        val second: Int,
        val third: Long
    )
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM