Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/228.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
Java 为什么调用API的函数返回空值或空值?_Java_Android_Firebase_Kotlin_Google Cloud Firestore - Fatal编程技术网

Java 为什么调用API的函数返回空值或空值?

Java 为什么调用API的函数返回空值或空值?,java,android,firebase,kotlin,google-cloud-firestore,Java,Android,Firebase,Kotlin,Google Cloud Firestore,(免责声明:当通过facebook、firebase等请求使用异步操作时,人们会提出大量关于数据为空/不正确的问题。我提出这个问题的目的是为每个在android中开始异步操作的人提供一个简单的答案) 我试图从我的一个操作中获取数据,当我使用断点或日志调试它时,值在那里,但是当我运行它时,它们总是空的,我如何解决这个问题 Firebase firebaseFirestore.collection("some collection").get() .addOnSuccessL

(免责声明:当通过facebook、firebase等请求使用异步操作时,人们会提出大量关于数据为空/不正确的问题。我提出这个问题的目的是为每个在android中开始异步操作的人提供一个简单的答案)

我试图从我的一个操作中获取数据,当我使用断点或日志调试它时,值在那里,但是当我运行它时,它们总是空的,我如何解决这个问题

Firebase

firebaseFirestore.collection("some collection").get()
            .addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
                @Override
                public void onSuccess(QuerySnapshot documentSnapshots) {
                     //I want to return these values I receive here? 
            })

等等。

什么是同步/异步操作?

那么,同步将等待任务完成。在这种情况下,代码执行“自上而下”

异步在后台完成任务,并在任务完成时通知您

如果希望通过方法/函数从异步操作返回值,可以在方法/函数中定义自己的回调,以便在从这些操作返回值时使用这些值

以下是Java的使用方法

首先定义一个接口:

interface Callback {
 void myResponseCallback(YourReturnType result);//whatever your return type is: string, integer, etc.
}
接下来,将方法签名更改为如下所示:

public void foo(final Callback callback) { // make your method, which was previously returning something, return void, and add in the new callback interface.
接下来,无论您以前想在哪里使用这些值,请添加以下行:

   callback.myResponseCallback(yourResponseObject);
例如:

 @Override
 public void onSuccess(QuerySnapshot documentSnapshots) {
 // create your object you want to return here
 String bar = document.get("something").toString();
 callback.myResponseCallback(bar);
 })
现在,在前面调用名为
foo
的方法的地方:

foo(new Callback() {
        @Override
        public void myResponseCallback(YourReturnType result) {
            //here, this result parameter that comes through is your api call result to use, so use this result right here to do any operation you previously wanted to do. 
        }
    });
}
您如何为Kotlin做到这一点? (作为一个基本示例,您只关心一个结果)

首先,将方法签名更改为以下内容:

fun foo(callback:(YourReturnType) -> Unit) {
.....
然后,在异步操作的结果中:

firestore.collection("something").document("document").get().addOnSuccessListener { 
                    val bar = it.get("something").toString()
                    callback.invoke(bar)
                }
然后,在以前调用名为
foo
的方法的地方,现在执行以下操作:

foo { result->
here, this result parameter that comes through is your api call result to use, so use this result right here to do any operation you previously wanted to do. 
}
如果您的
foo
方法以前接受了参数:

fun foo(value:SomeType, callback:(YourType) -> Unit)
您只需将其更改为:

foo(yourValueHere) { result ->
here, this result parameter that comes through is your api call result to use, so use this result right here to do any operation you previously wanted to do. 
    }
这些解决方案展示了如何创建一个方法/函数,从通过使用回调执行的异步操作中返回值


但是,重要的是要了解,如果您不想为以下内容创建方法/函数:

@Override
 public void onSuccess(SomeApiObjectType someApiResult) {
//here, this `onSuccess` callback provided by the api already has the data you're looking for (in this example, that data would be `someApiResult`).
//you can simply add all your relevant code which would be using this result inside this block here, this will include any manipulation of data, populating adapters, etc. 
//this is the only place where you will have access to the data returned by the api call, assuming your api follows this pattern
     })
@Override
 public void onSuccess(SomeApiObjectType someApiResult) {
//here, this `onSuccess` callback provided by the api already has the data you're looking for (in this example, that data would be `someApiResult`).
//you can simply add all your relevant code which would be using this result inside this block here, this will include any manipulation of data, populating adapters, etc. 
//this is the only place where you will have access to the data returned by the api call, assuming your api follows this pattern
     })