暂停用户酶与存储库
我有一个愚蠢的问题.....但是我找不到信息。
我创建了一个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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以提取 onsuccess 或
onerror
使用 运算符并检查返回的类型result
:我也将' t建议以小字母开头命名科特林课程,良好的风格是以大字母开头命名课程:
You can extract
onSuccess
oronError
usingwhen
operator and checking a type of the returnedresult
: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: