暂停用户酶与存储库

发布于 2025-02-05 16:14:45 字数 1440 浏览 2 评论 0原文

我有一个愚蠢的问题.....但是我找不到信息。

我创建了一个repository.kt,它使用Co-Routine

代码如下:

        override suspend fun getChallenge(): AwsResult<String> {
                return awsApi.getChallenge()
        }

AWSAPI零件如下:

override suspend fun getChallenge(): AwsResult<String> {
       ....

        return withContext(Dispatchers.IO) {
            try {
                val reponse: ... => API call through SDK
                AwsResult.onSuccess(reponse.text)
            } catch (e: Exception) {
                AwsResult.onError(e)
            }
        }
    }

我创建了挑战> clessiveusecase.kt在将结果发送回ViewModel之前,包含一点逻辑贴合。

     suspend fun registerDevice(): Result<String> {
        try {
            val result = repository.getChallenge()
        } catch (e: Exception) {

        }
    }

awsresult看起来像:

sealed class AwsResult<out R> {
    data class onSuccess<out T>(val data: T) : AwsResult<T>()
    data class onError(val exception: Exception) : AwsResult<Nothing>()
}

我想知道如何在用例文件中捕获awsresult?目的是提取信息,对此做点什么,然后将其发送回ViewModel实施的业务逻辑结果。我认为结果不会立即包含信息,并且我可能必须定义范围或周围的其他内容。

当我说,我认为结果不会立即包含信息,这是因为我尝试在usecase.kt中捕获OnSuccess and code> onResult

有什么想法吗?

I have a stupid question..... but I cannot find the information.

I have created a Repository.kt , which using Co-Routine

The code is as below:

        override suspend fun getChallenge(): AwsResult<String> {
                return awsApi.getChallenge()
        }

The AwsAPI part is done as below:

override suspend fun getChallenge(): AwsResult<String> {
       ....

        return withContext(Dispatchers.IO) {
            try {
                val reponse: ... => API call through SDK
                AwsResult.onSuccess(reponse.text)
            } catch (e: Exception) {
                AwsResult.onError(e)
            }
        }
    }

I have created a ChallengeUseCase.kt which will contain a little buit of logic before sending back the result to the ViewModel.

     suspend fun registerDevice(): Result<String> {
        try {
            val result = repository.getChallenge()
        } catch (e: Exception) {

        }
    }

and the AwsResult looks like:

sealed class AwsResult<out R> {
    data class onSuccess<out T>(val data: T) : AwsResult<T>()
    data class onError(val exception: Exception) : AwsResult<Nothing>()
}

I would like to know how do I catch the AwsResult in the use case file ? the goal is to extract an information, do something with it and the sending back to the viewModel the result of the business logic implemented. I assume that result will not contain right away the information and that I have to probably defined a scope or something around that.

When I say, that I assume that the result will not contain right away the info, it's because I try to catch the onSuccess and onResult in the UseCase.kt.

Any idea ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笙痞 2025-02-12 16:14:49

您可以提取 onsuccess 或onerror使用 运算符并检查返回的类型result

suspend fun registerDevice(): Result<String> {
    try {
        val result: AwsResult<String> = repository.getChallenge()
        when (result) {
            is AwsResult.onSuccess -> {
                val data = result.data
                // ... do something when request is success
            }
            is AwsResult.onError -> {
                val exception = result.exception
                // ... do something when request is error
            }
        }
        
    } catch (e: Exception) {

    }
}

我也将' t建议以小字母开头命名科特林课程,良好的风格是以大字母开头命名课程:

sealed class AwsResult<out R> {
    data class Success<out T>(val data: T) : AwsResult<T>()
    data class Error(val exception: Exception) : AwsResult<Nothing>()
}

You can extract onSuccess or onError using when operator and checking a type of the returned result:

suspend fun registerDevice(): Result<String> {
    try {
        val result: AwsResult<String> = repository.getChallenge()
        when (result) {
            is AwsResult.onSuccess -> {
                val data = result.data
                // ... do something when request is success
            }
            is AwsResult.onError -> {
                val exception = result.exception
                // ... do something when request is error
            }
        }
        
    } catch (e: Exception) {

    }
}

Also I wouldn't recommend to name Kotlin classes starting with a small letter, the good style is to name classes starting with a big letter:

sealed class AwsResult<out R> {
    data class Success<out T>(val data: T) : AwsResult<T>()
    data class Error(val exception: Exception) : AwsResult<Nothing>()
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文