如何从Android中的任何异步操作获取数据 [英] How to get data from any asynchronous operation in android

查看:576
本文介绍了如何从Android中的任何异步操作获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(免责声明:人们通过使用诸如Facebook,firebase等请求进行异步操作时询问数据是否为空/不正确,这引起了很多问题.我的目的是提供一个简单的答案对于所有人,从android中的异步操作开始的人来说,这个问题就解决了,因为我在搜索后无法真正找到一个示例)

我正在尝试从我的一项操作中获取数据,当我调试它时,值就在那里了,但是当我运行它们时它们总是为空,我该如何解决呢?

I'm trying to get data from one of my operations, when I debug it, the values are there, but when I run it they are always null, how can I solve this ?

Firebase

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

Facebook

GraphRequest request = GraphRequest.newGraphPathRequest(
            accessToken,
            "some path",
            new GraphRequest.Callback() {
                @Override
                public void onCompleted(GraphResponse response) {
                     //I want to get these values here? 
                }
            });
    request.executeAsync();

推荐答案

什么是同步/异步操作?

好吧,同步一直等到任务完成.在这种情况下,您的代码将执行自上而下"的操作.

Well, Synchronous waits until the task has completed. Your code executes "top-down" in this situation.

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

Asynchronous completes a task in the background and can notify you when it is complete.

要从异步操作中获取值,可以在方法中定义自己的回调,以使用这些值从这些操作返回时使用它们.

To get your values from an async operation, you can define your own callbacks in your methods to use these values as they are returned from these operations.

Java的使用方法

首先定义一个接口:

interface Callback {
 void myResponseCallback(YourReturnType result);//whatever your return type is: string, integer, etc.
}

接下来,将您的方法签名更改为:

next, change your method signature to be like this :

public void foo(final Callback callback) { // make your method, which was previously returning something, return void, and add in the new callback interface.

接下来,无论您以前想在哪里使用这些值,请添加以下行:

next up, wherever you previously wanted to use those values, add this line :

   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的方法:

now, where you were previously calling your method called 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做到这一点? (作为一个基本示例,其中您只关心单个结果)

How do you do this for Kotlin ? (as a basic example where you only care for a single result)

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

start off by changing your method signature to something like this:

fun foo(callback:(YourReturnType) -> Unit) {
.....

然后,在异步操作的结果中:

then, inside your asynchronous operation's result :

firestore.collection("something").document("document").get().addOnSuccessListener { 
                    val bar = it.get("something").toString()
                    callback.invoke(bar)
                }

然后,您以前在以前调用名为foo的方法的位置,现在执行此操作:

then, where you would have previously called your method called foo, you now do this :

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方法以前使用了参数:

if your foo method previously took in parameters :

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

这篇关于如何从Android中的任何异步操作获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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