首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >withContext总是返回false

withContext总是返回false
EN

Stack Overflow用户
提问于 2021-12-29 11:26:02
回答 1查看 41关注 0票数 0

ViewModel类:

代码语言:javascript
复制
            // 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
                ...
            }

我的存储库中的update函数正在执行中,我可以看到Firestore中的更改,没有问题。但是结果总是错误的,在我看来,ViewModel给出了一个错误的状态,我不喜欢它。我认为问题在withContext部分。我不完全理解它,但也许你可以给我关于如何解决这个问题的见解。我

这是我的AccountRepository类的代码:

代码语言:javascript
复制
// 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
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-12-29 11:37:49

当结果为null时,变量将更改为false,这意味着查询不成功。

我想这就是你错的地方。从文档来看,document.set()只返回一个在查询结束时将完成的任务,但它并没有说null将被用作失败的标志。

另外,您使用的等待函数将Task故障表示为抛出的异常(在协同中非常常见,我们通常将挂起函数中的故障与其他函数中的故障一样处理)。

如果您想要处理错误,您可能应该使用try-catch而不是if (result == null)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70518761

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档