Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/209.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何在代码中插入回调代码_Android_Firebase_Kotlin_Callback_Firebase Authentication - Fatal编程技术网

Android 如何在代码中插入回调代码

Android 如何在代码中插入回调代码,android,firebase,kotlin,callback,firebase-authentication,Android,Firebase,Kotlin,Callback,Firebase Authentication,当我在Android应用程序上创建JOIN Action和LOGIN Action时, Aa问题已经发生。 在登录操作中使用MVP模式。 但是登录结果并不是我想要的。 我给你看代码 class LoginModel { var TAG = "LoginModel" private var ID: String private var PW: String var resultTxt: String = "" var auth: FirebaseAuth

当我在Android应用程序上创建JOIN Action和LOGIN Action时, Aa问题已经发生。 在登录操作中使用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?
    }
}

将回调添加到登录函数中,该函数在设置
resultText
后被调用。以下几点应该行得通

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
}

作为回调尝试此操作,它将在执行后返回
resultText
的值

然后,当您调用登录方法时:

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
 }
}

检查我的答案,希望它能帮助你考虑这个问题并回答一个投票,这是一个基本的例子,如何在将来为人们做回调:D我专门为回调相关的问题做了这个帖子,如果你认为它有用的话,请考虑给它一个赞成票:D
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
 }
}