gpt4 book ai didi

android - android 中的二级回调不适用于嵌套查询

转载 作者:行者123 更新时间:2023-11-29 01:13:30 24 4
gpt4 key购买 nike

我创建了一个 geofire firebase 位置类。我能够从位置表中读取订户 key 。但是,现在我想查询订阅者表以获得完整的订阅者对象。

在 onGeoQueryReady 方法内部,我拥有所有订阅者 key 。但是当我在 onGeoQueryReady 方法中调用订阅者表时,它什么都不返回。

 public void findNearBySubscribers(final LocationInterface locationInterface, double radius, double clatitude, double clongitude){
DatabaseReference ref = FirebaseDatabase.getInstance().getReference("location/");
GeoFire geoFire = new GeoFire(ref);
final ArrayList<String> keys = new ArrayList<String>();
GeoQuery geoQuery = geoFire.queryAtLocation(new GeoLocation(clatitude, clongitude), radius);
geoQuery.addGeoQueryEventListener(new GeoQueryEventListener() {
@Override
public void onKeyEntered(String key, GeoLocation location) {
keys.add(key);
}

@Override
public void onKeyExited(String key) {
System.out.println(String.format("Key %s is no longer in the search area", key));
}

@Override
public void onKeyMoved(String key, GeoLocation location) {

}

@Override
public void onGeoQueryReady() {

locationInterface.getNearBySubscribersCallback(keys);
}

@Override
public void onGeoQueryError(DatabaseError error) {
System.err.println("There was an error with this query: " + error);
}
});
}

以下基于 key 查找订阅者的代码不起作用。订户列表大小为零,但响应字符串具有值。有人可以帮我解决这个问题吗?看起来这是一个 2 级回调...

 public void findNearBySubscribers(final LocationInterface locationInterface, double radius, double clatitude, double clongitude){
DatabaseReference ref = FirebaseDatabase.getInstance().getReference("location/");
GeoFire geoFire = new GeoFire(ref);
final ArrayList<String> keys = new ArrayList<String>();
GeoQuery geoQuery = geoFire.queryAtLocation(new GeoLocation(clatitude, clongitude), radius);
geoQuery.addGeoQueryEventListener(new GeoQueryEventListener() {
@Override
public void onKeyEntered(String key, GeoLocation location) {
keys.add(key);
}

@Override
public void onKeyExited(String key) {
System.out.println(String.format("Key %s is no longer in the search area", key));
}

@Override
public void onKeyMoved(String key, GeoLocation location) {

}

@Override
public void onGeoQueryReady() {
final ArrayList<Subscriber> subscriberList = new ArrayList<Subscriber>();

for(String key: keys) {
String url = "https://moe-90cc7.firebaseio.com/subscriber/";
String uri = url + key + ".json";
RestClient.get(uri, null, new TextHttpResponseHandler() {
@Override
public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
}

@Override
public void onSuccess(int statusCode, Header[] headers, String responseString) {
Gson gson = new Gson();
Subscriber subscriber = gson.fromJson(responseString, Subscriber.class);
System.out.println("Response Str" + responseString);
subscriberList.add(subscriber);
}
});
}
System.out.println("List size "+subscriberList.size());
locationInterface.getNearBySubscribersCallback(keys);
}

@Override
public void onGeoQueryError(DatabaseError error) {
System.err.println("There was an error with this query: " + error);
}
});
}

尝试了红色药丸方式 - 但它对我不起作用。基本上,只要订阅者 key 可用,我就会尝试加载订阅者。

package app.com.date.services;

import com.firebase.geofire.GeoFire;
import com.firebase.geofire.GeoLocation;
import com.firebase.geofire.GeoQuery;
import com.firebase.geofire.GeoQueryEventListener;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.gson.Gson;
import com.loopj.android.http.TextHttpResponseHandler;

import java.util.ArrayList;
import java.util.concurrent.Semaphore;

import app.com.date.subscriber.Subscriber;
import cz.msebera.android.httpclient.Header;

public class LocationService {
private ArrayList<Subscriber> subscriberList = new ArrayList<Subscriber>();

public void addGeoLocation(final LocationInterface locationInterface, String subscriberId, double latitude, double longitude) {

DatabaseReference ref = FirebaseDatabase.getInstance().getReference("location/");
GeoFire geoFire = new GeoFire(ref);
geoFire.setLocation(subscriberId, new GeoLocation(latitude, longitude), new GeoFire.CompletionListener() {
@Override
public void onComplete(String key, DatabaseError error) {
if (key != null) {
locationInterface.addLocationCallback(true);
} else {
locationInterface.addLocationCallback(false);
}
}
});
}

public void findNearBySubscribers(final LocationInterface locationInterface, double radius, double clatitude, double clongitude) {
DatabaseReference ref = FirebaseDatabase.getInstance().getReference("location/");
GeoFire geoFire = new GeoFire(ref);
final ArrayList<String> keys = new ArrayList<String>();
final ArrayList<Subscriber> subscriberList = new ArrayList<Subscriber>();
GeoQuery geoQuery = geoFire.queryAtLocation(new GeoLocation(clatitude, clongitude), radius);
geoQuery.addGeoQueryEventListener(new GeoQueryEventListener() {
@Override
public void onKeyEntered(String key, GeoLocation location) {
String url = "https://moe-90cc7.firebaseio.com/subscriber/";
String uri = url + key + ".json";
RestClient.get(uri, null, new TextHttpResponseHandler() {
@Override
public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
}
@Override
public void onSuccess(int statusCode, Header[] headers, String responseString) {
Gson gson = new Gson();
Subscriber subscriber = gson.fromJson(responseString, Subscriber.class);
System.out.println("Response Str" + responseString);
subscriberList.add(subscriber);
}
});

System.out.println("onKeyEntered list "+subscriberList.size());

}

@Override
public void onKeyExited(String key) {
System.out.println(String.format("Key %s is no longer in the search area", key));
}

@Override
public void onKeyMoved(String key, GeoLocation location) {

}

@Override
public void onGeoQueryReady() {
System.out.println("onGeoQueryReady list "+subscriberList.size());
}

@Override
public void onGeoQueryError(DatabaseError error) {
System.err.println("There was an error with this query: " + error);
}
});
}
}

输出

12-31 17:57:34.090 13956-14019/app.com.date V/FA: Processing queued up service tasks: 3
12-31 17:57:34.366 13956-13956/app.com.date I/System.out: onKeyEntered list 0
12-31 17:57:34.366 13956-13956/app.com.date I/System.out: onKeyEntered list 0
12-31 17:57:34.366 13956-13956/app.com.date I/System.out: onGeoQueryReady list 0
12-31 17:57:34.456 13956-13956/app.com.date V/AsyncHttpRH: Progress 332 from 332 (100%)
12-31 17:57:34.475 13956-13956/app.com.date I/System.out: Response Str{"age":29,"alcohol":false,"animalLover":false,"children":false,"id":"GYlSDXx0Kwh7AerOzFGf8MDmhOg1","isEmpty":false,"mainImage":"https://firebasestorage.googleapis.com/v0/b/moe-erOzFGf8MDmhOg1%2F1265290048?alt=media&token=80ab1410-bac9-4679-a3d4-152204319310","name":"singh","smoker":false}
12-31 17:57:34.574 13956-13956/app.com.date V/AsyncHttpRH: Progress 327 from 327 (100%)
12-31 17:57:34.584 13956-13956/app.com.date I/System.out: Response Str{"age":65,"alcohol":false,"animalLover":false,"children":false,"id":"xZWPMjWQCYY4xNimx3WHqVZcQxs1","isEmpty":false,"mainImage":"https://firebasestorage.googleapQCYY4xNimx3WHqVZcQxs1%2F1405868067?alt=media&token=b8d128a6-7156-42e8-9d0b-953feb128b4c","name":" Singh","smoker":false}
12-31 17:57:39.123 13956-14019/app.com.date V/FA: Inactivity, disconnecting from the service

最佳答案

问题出在这段代码中:

final ArrayList<Subscriber> subscriberList = new ArrayList<Subscriber>();

for(String key: keys) {
String url = "https://moe-90cc7.firebaseio.com/subscriber/";
String uri = url + key + ".json";
RestClient.get(uri, null, new TextHttpResponseHandler() {
@Override
public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
}

@Override
public void onSuccess(int statusCode, Header[] headers, String responseString) {
Gson gson = new Gson();
Subscriber subscriber = gson.fromJson(responseString, Subscriber.class);
System.out.println("Response Str" + responseString);
subscriberList.add(subscriber);
}
});
}
System.out.println("List size "+subscriberList.size());
locationInterface.getNearBySubscribersCallback(keys);

数据从 Firebase 加载异步。这意味着当您打印 subscriberList.size() 时,您还没有加载任何订阅者。

如果您更改代码以输出一些简单的日志记录语句,这是最容易看到的:

final ArrayList<Subscriber> subscriberList = new ArrayList<Subscriber>();

System.out.println("Before starting to load");
for(String key: keys) {
String url = "https://moe-90cc7.firebaseio.com/subscriber/";
String uri = url + key + ".json";
RestClient.get(uri, null, new TextHttpResponseHandler() {
@Override
public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
}

@Override
public void onSuccess(int statusCode, Header[] headers, String responseString) {
Gson gson = new Gson();
Subscriber subscriber = gson.fromJson(responseString, Subscriber.class);
System.out.println("Response Str" + responseString);
subscriberList.add(subscriber);
System.out.println("In onSuccess, list size "+subscriberList.size());
}
});
}
System.out.println("After starting to load, list size "+subscriberList.size());
locationInterface.getNearBySubscribersCallback(keys);

这个的输出将是:

Before starting to load

After starting to load, list size 0

In onSuccess, list size 1

In onSuccess, list size 2

...

因此您可以看到 onSuccess() 中的代码在文件中较低的代码之后运行。请记住:这是因为数据是异步加载的,并且在处理现代网络/云 API 时是完全正常的行为。

解决方案是重构您的问题。与其说“首先加载订阅者,然后对它们执行 xyz”,不如尝试将您的问题重新定义为“只要订阅者发生变化,就对它们执行 xyz”。

它的实现是将对订阅者执行某些操作的代码移到 onSuccess 处理程序中,就像我打印订阅者的长度一样列表。

关于android - android 中的二级回调不适用于嵌套查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41406915/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com