【问题标题】:how can i insert CALLBACK code in my code如何在我的代码中插入 CALLBACK 代码
【发布时间】: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?
    }
}

【问题讨论】:

标签: android firebase kotlin callback firebase-authentication


【解决方案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
    }
    
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-11
    • 2018-06-04
    • 1970-01-01
    • 1970-01-01
    • 2017-05-15
    相关资源
    最近更新 更多