withContext 总是返回 false
withContext is always returning false
ViewModel class:
// Update the userInformation and returns Boolean if the operation succeed
val res = viewModelScope.async {
accountRepository.update(
userId,
firstName,
lastName,
birthDate,
photoUrl
)
}
// This should execute
// But the result is always false and go to the else part.
if (res.await()) {
...
} else {
// But it always goes here
...
}
我的仓库中的更新功能正在执行,我可以看到Firestore中的变化,没有问题。但结果总是错误的,ViewModel 在我看来给出了错误的状态,我不喜欢它。我认为问题出在 withContext 部分。我不完全理解,但也许你可以给我一些关于如何解决这个问题的见解。我
这是我的 AccountRepository 的代码 class:
// Account Repository
// Update the user information
suspend fun update(
userId: String,
firstName: String,
lastName: String,
birthDate: String,
avatarUrl: String
): Boolean {
return withContext(dispatcher) {
// This will be true for now
var isSuccess = true
// Parsing the date string "01/01/2000" to a date object
val date = SimpleDateFormat("MM/dd/yyyy", Locale.ENGLISH).parse(birthDate)
// Creating an instance of calendar object
val calendarDate = Calendar.getInstance()
// Set the time with the date variable
calendarDate.time = date!!
// Check if the user is in the right age to use the application
// User should be 12 years old and above to access this app
if (Calendar.getInstance().get(Calendar.YEAR)
.minus(calendarDate.get(Calendar.YEAR)) >= 12
) {
// Create a map to update fields in the firebase
val updateUserMap = mapOf<String, Any>(
"avatarUrl" to avatarUrl,
"firstName" to firstName,
"lastName" to lastName,
"birthDate" to birthDate,
"dateModified" to Utils.getTimeInMillisUTC()
)
// Execute the query in fireStore
// I'm using ktx gradle library
// This line executes. It reflects the changes in the fireStore
val result = userCollectionRef
.document(userId)
.set(updateUserMap, SetOptions.merge())
.await()
// The variable will be changed to false when the result is null
// Meaning that the query is not successful
if (result == null) {
isSuccess = false
}
}
// Return the variable after all operations
isSuccess
}
}
The variable will be changed to false when the result is null
Meaning that the query is not successful
我认为这是你错的地方。从docs来看,好像document.set()
只是returns一个task,会在query结束的时候完成,但是并没有说null
会被用作marker of失败。
此外,您正在使用的 await 函数会将 Task
失败表示为抛出的异常(这在协程中很常见,我们通常将挂起函数中的失败视为其他函数中的失败函数)。
如果你想处理错误,你应该使用 try-catch
而不是 if (result == null)
。
ViewModel class:
// Update the userInformation and returns Boolean if the operation succeed
val res = viewModelScope.async {
accountRepository.update(
userId,
firstName,
lastName,
birthDate,
photoUrl
)
}
// This should execute
// But the result is always false and go to the else part.
if (res.await()) {
...
} else {
// But it always goes here
...
}
我的仓库中的更新功能正在执行,我可以看到Firestore中的变化,没有问题。但结果总是错误的,ViewModel 在我看来给出了错误的状态,我不喜欢它。我认为问题出在 withContext 部分。我不完全理解,但也许你可以给我一些关于如何解决这个问题的见解。我
这是我的 AccountRepository 的代码 class:
// Account Repository
// Update the user information
suspend fun update(
userId: String,
firstName: String,
lastName: String,
birthDate: String,
avatarUrl: String
): Boolean {
return withContext(dispatcher) {
// This will be true for now
var isSuccess = true
// Parsing the date string "01/01/2000" to a date object
val date = SimpleDateFormat("MM/dd/yyyy", Locale.ENGLISH).parse(birthDate)
// Creating an instance of calendar object
val calendarDate = Calendar.getInstance()
// Set the time with the date variable
calendarDate.time = date!!
// Check if the user is in the right age to use the application
// User should be 12 years old and above to access this app
if (Calendar.getInstance().get(Calendar.YEAR)
.minus(calendarDate.get(Calendar.YEAR)) >= 12
) {
// Create a map to update fields in the firebase
val updateUserMap = mapOf<String, Any>(
"avatarUrl" to avatarUrl,
"firstName" to firstName,
"lastName" to lastName,
"birthDate" to birthDate,
"dateModified" to Utils.getTimeInMillisUTC()
)
// Execute the query in fireStore
// I'm using ktx gradle library
// This line executes. It reflects the changes in the fireStore
val result = userCollectionRef
.document(userId)
.set(updateUserMap, SetOptions.merge())
.await()
// The variable will be changed to false when the result is null
// Meaning that the query is not successful
if (result == null) {
isSuccess = false
}
}
// Return the variable after all operations
isSuccess
}
}
The variable will be changed to false when the result is null Meaning that the query is not successful
我认为这是你错的地方。从docs来看,好像document.set()
只是returns一个task,会在query结束的时候完成,但是并没有说null
会被用作marker of失败。
此外,您正在使用的 await 函数会将 Task
失败表示为抛出的异常(这在协程中很常见,我们通常将挂起函数中的失败视为其他函数中的失败函数)。
如果你想处理错误,你应该使用 try-catch
而不是 if (result == null)
。