Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/192.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_Kotlin - Fatal编程技术网

Android 我希望只有在收到实时数据后才能调用函数

Android 我希望只有在收到实时数据后才能调用函数,android,kotlin,Android,Kotlin,我在安卓系统中使用实时数据。但我的问题是,在调用每个函数之后,最终都会收到数据。在我的例子中,我的一个函数依赖于实时数据,但它是在收到实时数据之前调用的。我添加了注释,以使您更好地理解。请帮忙 // polist is a MutableList transactionDao.selectAll().observe(this, Observer { if (it != null && it.isNotEmpty()) {

我在安卓系统中使用实时数据。但我的问题是,在调用每个函数之后,最终都会收到数据。在我的例子中,我的一个函数依赖于实时数据,但它是在收到实时数据之前调用的。我添加了注释,以使您更好地理解。请帮忙

// polist is a MutableList
 transactionDao.selectAll().observe(this, Observer {
                if (it != null && it.isNotEmpty()) {
                   polist.addAll(it)
                }
            })
 vregularDao.getAll().observe(this, Observer {
                if (it != null && it.isNotEmpty()) {
                   polist.addAll(it)
                }
            })
// but this is called first then above codes.I want this to be called only after live data is received
 alllist.forEach{
         //perform some action   
        }       

 

您可能需要查看线程。下面的代码在线程中运行,一旦有返回值,它就会被添加到
polist

transactionDao.selectAll().observe(this, Observer {
                if (it != null && it.isNotEmpty()) {
                   polist.addAll(it)
                }
            })
同样适用于

 vregularDao.getAll().observe(this, Observer {
                if (it != null && it.isNotEmpty()) {
                   polist.addAll(it)
                }
            })
因此,直接调用最后一段代码。因为另外两个线程目前还没有返回任何数据

您需要创建某种拦截器

所以在列表枚举之后调用另一个函数,这不是优雅的(PSUDEOCODE)


美好的但这在我的情况下不起作用,因为一个数据可能为空,在这种情况下,我的函数将永远不会运行。我希望它即使有一个数据为空也能运行。顺便说一句,我有5个实时数据调用,我只显示了2个。在这种情况下,删除布尔检查。每次您获得更多数据时,它都会重做您的
alllist.foreach
,这可能是一个很好的解决方案,但我只希望func运行once@NoobDev那么我认为获取您使用的数据的函数是不正确的。当包含您正在使用的数据的数据库发生任何更改时,您的函数将运行。
    bool selectAllDone;
    bool getAllDone;
    // polist is a MutableList
 transactionDao.selectAll().observe(this, Observer {
                if (it != null && it.isNotEmpty()) {
                   polist.addAll(it)
                   performSomeAction();
                   selectAllDone = true;
                }
            })
 vregularDao.getAll().observe(this, Observer {
                if (it != null && it.isNotEmpty()) {
                   polist.addAll(it)
                   performSomeAction();
                   getAllDone = true;
                }
            })
    // but this is called first then above codes.I want this to be called only after live data is received
     public fun performSomeAction(){
if(getAllDone & selectAllDone){
     alllist.forEach{
             //perform some action   
            }  
}
}