Firebase完成回调在Kotlin中如何工作 [英] How does Firebase completion callbacks work in Kotlin

查看:91
本文介绍了Firebase完成回调在Kotlin中如何工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经成功地从Firebase读取了数据,并使用完成回调将其异步显示在imageView中.作为编程新手,我仍然很难理解有关回调的一些机制,并希望有人会为我的某些问题提供一些启示.我已经阅读并观看了多个教程和解释,但是仍然难以理解某些概念.这是我的Firebase代码:

I have successfully read data from Firebase and displayed it in an imageView asynchronously using a completion callback. Being new to programming, I'm still having a hard time understanding some of the mechanisms around callbacks and hope someone would be kind to shed some light to some of my questions. I have already read and watched multiple tutorials and explanations, but still struggle understanding some concepts. Here is my Firebase code:

第1部分:

readFirebaseData(object: FirebaseCallback{
        override fun onCallback(list: MutableList<RecipeTemplate>) {

            glideVariable?.loadImageUrl(recipeArray[1].recipeImage) //WORKS!!
        }
})

第2部分:

fun readFirebaseData(firebaseCallback: FirebaseCallback) {
    ref!!.addValueEventListener(object: ValueEventListener {
        override fun onDataChange(snapshot: DataSnapshot?) {

            for (item in snapshot!!.children) {
                var tempRecipe = RecipeTemplate()
                val image = item.child("recipeImageFirebase")

                tempRecipe.recipeImage = image.value.toString()
                recipeArray.add(tempRecipe)
            }
            //INSERTING CODE HERE AT LATER STAGE... SEE LATER IN POST
            firebaseCallback.onCallback(recipeArray)

        }//END ON DATA CHANGE METHOD
    }) //END FB
}//END READ DATA

第3部分:

interface FirebaseCallback {
    fun onCallback(list: MutableList<RecipeTemplate>)
}

主要是我不了解接口的要点以及整个替代功能部分(PART 1).事实是,仅使用Firebase代码末尾的函数调用,我就成功地完成了同样的事情.它所需的代码更少,并且易于理解和理解,并且据我所知,它具有相同的作用.看起来像这样:

The main thing is that I don't understand the point of the interface, and the whole override function-part (PART 1). Thing is, I successfully did the very same thing just using a function call at the end of the Firebase code. It requires way less code and is easier to follow and understand, and from what I can tell, it does the same thing. It looks like this:

第2部分:

test(recipeArray) //SIMPLY CALLING A FUNCTION INSTEAD OF THE CODE PREVIOUSLY USED.
//firebaseCallback.onCallback(recipeArray)

然后测试功能本身:

 fun test(list: MutableList<RecipeTemplate>) {
    Log.d("TAGM", "DONE WITH FB")
    glideVariable?.loadImageUrl(recipeArray[3].recipeImage)    
}

那么,我在这里想念什么?为什么要使用整个界面回调呢?

So, what am I missing here? Why use the whole interface callback thing?

推荐答案

要解决此问题,您需要创建自己的回调以等待Firebase返回数据.为此,首先需要创建一个如下的interface:

To solve this, you need to create your own callback to wait for Firebase to return you the data. To achieve this, first you need to create an interface like this:

interface FirebaseCallback {
    fun onCallback(list: MutableList<RecipeTemplate>)
}

然后,您需要创建一个实际上从数据库获取数据的函数.此函数应如下所示:

Then you need to create a function that is actually getting the data from the database. This function should look like this:

fun readFirebaseData(firebaseCallback: FirebaseCallback) {
    ref.addListenerForSingleValueEvent(object : ValueEventListener {
        override fun onDataChange(dataSnapshot: DataSnapshot) {
            val list = ArrayList<RecipeTemplate>()
            for (ds in dataSnapshot.getChildren()) {
                val recipeTemplate = ds.getValue(RecipeTemplate::class.java!!)
                list.add(recipeTemplate)
            }
            firebaseCallback.onCallback(list)
        }

        override fun onCancelled(databaseError: DatabaseError) {}
    })
}

最后,只需调用readData()函数,并在需要的地方将FirebaseCallback接口的实例作为参数传递,如下所示:

In the end just simply call readData() function and pass an instance of the FirebaseCallback interface as an argument wherever you need it like this:

readFirebaseData(object : FirebaseCallback {
    override fun onCallback(list: MutableList<RecipeTemplate>) {
        //Do what you need to do with your list
    }
})

这是您可以在onDataChange()函数之外使用该值的唯一方法.

This is the only way in which you can use that value outside onDataChange() function.

这篇关于Firebase完成回调在Kotlin中如何工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆