Kotlin Firebase实时数据库中MVVM的setValue()方法的正确返回值

huangapple go评论80阅读模式
英文:

Kotlin Firebase Realtime Database correct return value for setValue() in MVVM

问题

HomeRepository 类中的 setMyBirthday 函数的正确返回值应该是 Unit,而不是 Void。以下是修改后的代码:

suspend fun setMyBirthday(birthday: String) {
    databaseReference
        .child("users")
        .child(myUserID)
        .child("birthday")
        .setValue(birthday)
        .await()
}

这个函数没有明确的返回值,因为它是一个挂起函数,只是执行了数据库操作而没有返回数据。

英文:

What would be the correct return value of my setMyBirthday function in the HomeRepository class for the code to work? Void doesn't seem to be the right return value.

Fragment:

private fun HomeViewModel.setupObserver() {
    myBirthday.observe(viewLifecycleOwner) { response ->
        when(response) {
            is Resource.Error -> {
                response.message?.let { message ->
                    Log.e(TAG, "An error occurred: $message")
                }
            }
            is Resource.Loading -> {
                binding.buttonChangeBirthday.isClickable = false
            }
            is Resource.Success -> {
                binding.buttonChangeBirthday.isClickable = true
            }
        }
    }
}

HomeViewModel:

val myBirthday: MutableLiveData<Resource<Void>> = MutableLiveData()
fun setMyBirthday(birthday: String) = viewModelScope.launch {
    try {
        myBirthday.postValue(Resource.Loading())
        val response = homeRepository.setMyBirthday(birthday)
        myBirthday.postValue(Resource.Success(response))
    } catch (e: Exception) {
        myBirthday.postValue(Resource.Error(e.message!!))
    }
}

HomeRepository:

    suspend fun setMyBirthday(birthday: String) =
    databaseReference
        .child("users")
        .child(myUserID)
        .child("birthday")
        .setValue(birthday)
        .await()

答案1

得分: 1

以下是翻译好的部分:

"Indeed Void is the type of object the setMyBirthday function returns. So your function should look like this:

// 👇
suspend fun setMyBirthday(birthday: String): Void =
databaseReference
.child("users")
.child(myUserID)
.child("birthday")
.setValue(birthday)
.await()

While your approach will work, it will be more convenient to return a Resource<Boolean> as in the following lines of code:

suspend fun setMyBirthday(birthday: String): Resource<Boolean> = try {
databaseReference
.child("users")
.child(myUserID)
.child("birthday")
.setValue(birthday)
.await()
Resource.Success(true)
} catch (e: Exception) {
Resource.Error(e)
}

This is more useful because you can pass the error further, otherwise, you won't know if something fails.

英文:

> What would be the correct return value of my function in the HomeRepository class for the code to work? Void doesn't seem to be the right return value.

Indeed Void is the type of object the setMyBirthday function returns. So your function should look like this:

//                                            &#128071;
suspend fun setMyBirthday(birthday: String): Void =
databaseReference
    .child(&quot;users&quot;)
    .child(myUserID)
    .child(&quot;birthday&quot;)
    .setValue(birthday)
    .await()

While your approach will work, it will be more convenient to return a Resource&lt;Boolean&gt; as in the following lines of code:

suspend fun setMyBirthday(birthday: String): Resource&lt;Boolean&gt; = try {
    databaseReference
        .child(&quot;users&quot;)
        .child(myUserID)
        .child(&quot;birthday&quot;)
        .setValue(birthday)
        .await()
    Resource.Success(true)
} catch (e: Exception) {
    Resource.Error(e)
}

This is more useful because you can pass the error further, otherwise, you won't know if something fails.

huangapple
  • 本文由 发表于 2023年6月2日 00:48:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76384085.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定