英文:
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:
// 👇
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论