如何在我的代码中插入 CALLBACK 代码
Posted
技术标签:
【中文标题】如何在我的代码中插入 CALLBACK 代码【英文标题】:how can i insert CALLBACK code in my code 【发布时间】:2019-11-29 15:05:23 【问题描述】:当我在 android 应用上创建 JOIN Action 和 LOGIN Action 时, 发生了问题。 在 LOGIN Action 中使用 MVP 模式。 但是登录的结果并不是我想要的。 我给你看代码。
class LoginModel
var TAG = "LoginModel"
private var ID: String
private var PW: String
var resultTxt: String = ""
var auth: FirebaseAuth = FirebaseAuth.getInstance()
constructor(ID: String, PW: String)
this.ID = ID
this.PW = PW
fun login(ID: String, PW: String) : String
this.ID = ID
this.PW = PW
auth.signInWithEmailAndPassword(ID, PW)
.addOnCompleteListener task ->
//
if (task.isSuccessful)
val user = auth.currentUser
resultTxt = "Login Success"
else
resultTxt = "Login Failed"
return resultTxt
// I'd like to process the results based on the return.
// But it doesn't return the way I want it.
// I know it's related to asynchronous processing.
// So where should I put the callback function, and how should I write
// it?
【问题讨论】:
查看我的回答,希望对您有所帮助 请考虑给这个问题并回答一个upvote,这是未来如何为人们进行回调的基本示例:D ***.com/questions/57330766/… 【参考方案1】:fun login(ID: String, PW: String, callback:(String) -> Unit)
this.ID = ID
this.PW = PW
auth.signInWithEmailAndPassword(ID, PW)
.addOnCompleteListener task ->
if (task.isSuccessful)
val user = auth.currentUser
resultTxt = "Login Success"
else
resultTxt = "Login Failed"
callback.invoke(resultTxt)
尝试将此作为回调,它会在执行后返回resultTxt
的值。
然后,当你调用登录方法时:
login(ID,PW) result ->
//here, result is the value of the callback
交替
您可以在回调中返回调用的结果和用户,如下所示:
fun login(ID: String, PW: String, callback:(Boolean, User?) -> Unit)
this.ID = ID
this.PW = PW
auth.signInWithEmailAndPassword(ID, PW)
.addOnCompleteListener task ->
if (task.isSuccessful)
callback.invoke(true, auth.currentUser)
else
callback.invoke(false, null)
然后你可以这样使用它:
Login(id, password) result: Boolean, user: User? ->
if(result)
//the result is successful
user?.let authUser ->
//here, authUser is your user
else
//the result was not successful
【讨论】:
【参考方案2】:为您的登录函数添加一个回调,在设置resultTxt
后调用该函数。以下几行应该可以工作,
class LoginModel
var TAG = "LoginModel"
private var ID: String
private var PW: String
var resultTxt: String = ""
var auth: FirebaseAuth = FirebaseAuth.getInstance()
constructor(ID: String, PW: String)
this.ID = ID
this.PW = PW
fun login(ID: String, PW: String, callback: (String)->Unit)
this.ID = ID
this.PW = PW
auth.signInWithEmailAndPassword(ID, PW)
.addOnCompleteListener task ->
//
if (task.isSuccessful)
val user = auth.currentUser
resultTxt = "Login Success"
else
resultTxt = "Login Failed"
//The callback get's invoked with your expected result value.
callback.invoke(resultTxt)
//Don't return here
//return resultTxt
// I'd like to process the results based on the return.
// But it doesn't return the way I want it.
// I know it's related to asynchronous processing.
// So where should I put the callback function, and how should I write
// it?
然后您可以使用以下方法调用该方法,
login(id,password) result ->
//Do what you want with the result here
【讨论】:
我专门针对与回调相关的问题发表了这篇文章,如果您认为它可能有用,请考虑给它投票:D ***.com/questions/57330766/…以上是关于如何在我的代码中插入 CALLBACK 代码的主要内容,如果未能解决你的问题,请参考以下文章
在我的 stc.StyledTextCtrl 中插入一个空格,这样代码就不会那么接近行号
如何使用 ItemTouchHelper 及其 Callback 或实现自定义的?