我有一个用户登录的函数。但它被暂停了。我试着获取它的返回值,但我做不到。这就是我想做的
代码
class LoginViewModel @ViewModelInject constructor(private val remoteDataSource: OrderRemoteDataSource) :
ViewModel() {
private fun areValidCredentials(username: String?, password: String?): Boolean {
return username != null && password != null && username.length > 4 && password.length > 4
}
suspend fun login(username: String?, password: String?): Boolean {
return suspendCoroutine { it ->
val valid = areValidCredentials(username, password)
if (valid) {
// call finish so login activity won't show up after back button clicked in home fragment
try {
viewModelScope.launch {
//TODO CHECK if error code
val loginResponse =
remoteDataSource.login(LoginRequest(username!!, password!!))
if (loginResponse.status == Resource.Status.SUCCESS) {
val jwtToken = loginResponse.data?.jwtToken
if (!jwtToken.isNullOrEmpty()) {
sessionManager.saveAuthToken(jwtToken!!)
//ERROR!
it.resume(true)
}
}
}
} catch (e: Exception) {
Log.i("[LoginActivity]", e.localizedMessage!!)
it.resume(false)
e.printStackTrace()
}
} else {
Toast.makeText(
LOGIN_ACTIVITY,
"Username and password must be at least 5 letters long",
Toast.LENGTH_SHORT
).show()
}
it.resume(false)
}
}
}
我称之为
@AndroidEntryPoint
class LoginFragment : Fragment() {
private val mViewModel: LoginViewModel by viewModels()
private lateinit var navController: NavController
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
return inflater.inflate(R.layout.frg_login, container, false)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Log.d("[LoginFragment]", "onCreate fun started!")
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
navController = findNavController()
loginButton.setOnClickListener {
//TODO navigate to new fragmnet
lifecycleScope.launch {
mViewModel.login(
loginUsernameText.text.toString(),
loginPasswordText.text.toString()
)
}
}
}
我有错误
E/AndroidRuntime:致命异常:主进程:ru.gkomega.navigation,pid:11863java.lang.IllegalStateException:已在kotlin.coroutines.safeContinuation.ResumeWith(safeContinuationJVM.kt:45)在ru.gkomega.maumarket.ui.login.loginviewModel$login$$inlined$suspendCoroutines.jvm.internal.baseContinuationImpl.ResumeWith(continuationImpl.kt:33)在uest对不可优化的方法java.lang.Class java.lang.Class.ClassForName(java.lang.String,boolean,java.lang.ClassLoader)I/process:发送信号。PID:11863 SIG:9与目标VM断开连接,地址:“localhost:58264",传输:”socket“
我不是很了解coroutine,所以它们可能是问题所在
无论请求是否发生,是否失败,您都将恢复协调线。
suspend fun login(username: String?, password: String?): Boolean = suspendCoroutine { cont ->
if (areValidCredentials(username, password)) {
try {
viewModelScope.launch {
val loginResponse = remoteDataSource.login(LoginRequest(username!!, password!!))
val jwtToken = loginResponse.data?.jwtToken
if (loginResponse.status == Resource.Status.SUCCESS && !jwtToken.isNullOrEmpty()) {
sessionManager.saveAuthToken(jwtToken!!)
cont.resume(true)
} else cont.resume(false) // <-- Don't forget
}
} catch (e: Exception) {
Log.i("[LoginActivity]", e.localizedMessage!!)
cont.resume(false)
e.printStackTrace()
}
} else {
Toast.makeText(
LOGIN_ACTIVITY,
"Username and password must be at least 5 letters long",
Toast.LENGTH_SHORT
).show()
cont.resume(false) // <-- Put it right here.
}
// cont.resume(false) // <-- Not here
}
如何在使用Kotlin的Spring Boot中正确初始化ConfigurationProperties? 目前我喜欢下面的例子: 但是它看起来很丑陋,实际上不是一个iable,foo是常量ue,应该在启动期间初始化,将来不会改变。
在kotlin中定义一个有公共getter和私有(只有内部可修改)setter的var的正确方法是什么?
问题内容: 我正在学习具有C ++和Java背景的Kotlin。我期待下面的打印,不。我知道这对应到。默认实现不比较每个成员,即和吗?如果是这样,它会不会看到字符串值相等(因为再次映射到字符串值)?显然,我在Kotlin中还没有涉及平等与身份相关的问题。 问题答案: 您描述的默认实现仅适用于数据类。不适用于从中继承实现的常规类,只需使对象与自身相等即可。
这是正确的吗? 我可以找到一个相关的问题,但它是有参数的,我不能在没有params的情况下转换它。