Android 如何通过代码变量而不是云变量(查询)更改firestore回收商订单?

Android 如何通过代码变量而不是云变量(查询)更改firestore回收商订单?,android,firebase,google-cloud-firestore,Android,Firebase,Google Cloud Firestore,我在Firestore回收器中插入了一个对象列表,我需要根据当前用户的距离和用户与对象的距离对它们进行排序,我能够通过获取用户的当前位置并提取相关对象发布者的位置来计算它们之间的距离,它返回它们之间的距离(以米为单位),我想使用它作为参数,根据获得的值对数据进行排序,但是,对获得的数据进行排序的唯一方法是使用此文档firestore 但是它花费了我很多,而且不是很有效,我能做些什么来根据我创建的这个局部距离变量改变列出的对象的顺序吗 范例 if(locationResult != null &

我在Firestore回收器中插入了一个对象列表,我需要根据当前用户的距离和用户与对象的距离对它们进行排序,我能够通过获取用户的当前位置并提取相关对象发布者的位置来计算它们之间的距离,它返回它们之间的距离(以米为单位),我想使用它作为参数,根据获得的值对数据进行排序,但是,对获得的数据进行排序的唯一方法是使用此文档firestore

但是它花费了我很多,而且不是很有效,我能做些什么来根据我创建的这个局部距离变量改变列出的对象的顺序吗

范例

 if(locationResult != null && locationResult.getLocations().size() > 0){

    int latestLocationIndex = locationResult.getLocations().size() - 1;
    latitudeUser = locationResult.getLocations().get(latestLocationIndex).getLatitude();
    longitudeUser = locationResult.getLocations().get(latestLocationIndex).getLongitude();
    localizacaoUser.setLatitude(latitudeUser); // Latitude do usuário adquirida
    localizacaoUser.setLongitude(longitudeUser); 
    query.addSnapshotListener(new EventListener<QuerySnapshot>() {
        @Override
        public void onEvent(@Nullable QuerySnapshot documentSnapshots, @Nullable FirebaseFirestoreException e) {
            for (DocumentChange doc : documentSnapshots.getDocumentChanges()){
                if (doc.getType() == DocumentChange.Type.ADDED){

                    AnuncioPrincipal anuncioPrincipal1 = doc.getDocument().toObject(AnuncioPrincipal.class);
                    Location localAnuncioAtual = new Location("ProviderAun");

                    localAnuncioAtual.setLatitude(anuncioPrincipal1.getLatAnun());
                    localAnuncioAtual.setLongitude(anuncioPrincipal1.getLongAnun());
                    Log.i("valor2848", "onEvent: "+localizacaoUser.distanceTo(localAnuncioAtual)/1000+" Km Used by" +
                            anuncioPrincipal1.getCidadeAnunciante()); // CityUser - Actual City

                }
            }
        }
    });

if(locationResult!=null&&locationResult.getLocations().size()>0){
int latestLocationIndex=locationResult.getLocations().size()-1;
latitudeUser=locationResult.getLocations().get(latestLocationIndex.getLatitude();
longitudeUser=locationResult.getLocations().get(latestLocationIndex.getLongitude();
LocalizacaUser.setLatitude(latitudeUser);//usuário adquirida纬度
LocalizaoUser.SetLength(经度);
addSnapshotListener(新的EventListener(){
@凌驾
public void onEvent(@Nullable QuerySnapshot documentSnapshots,@Nullable FirebaseFirestoreException e){
对于(DocumentChange文档:documentSnapshots.getDocumentChanges()){
if(doc.getType()==DocumentChange.Type.ADDED){
AnuncioPrincipal anuncioPrincipal1=doc.getDocument().toObject(AnuncioPrincipal.class);
地点localAnuncioAtual=新地点(“ProviderAun”);
localAnuncioAtual.setLatitude(anuncioPrincipal1.getLatAnun());
localAnuncioAtual.setLongitude(anuncioPrincipal1.getLongAnun());
Log.i(“valor2848”,“OneEvent:+localizacaoUser.distanceTo(LocalAnuncious)/1000+“使用公里”+
anuncioPrincipal1.getcidadeanuciante();//城市用户-实际城市
}
}
}
});

基本上,我想添加一个额外的字段,用于对回收器中列出的数据进行排序,但该字段不在Firestore中,因为如果它在Firestore中,每个用户每分钟都会更改此变量。

您可以向
AnuncioPrincipal
添加一个不从Firestore读取(或写入)的字段:

public class AnuncioPrincipal {
    ...

    @Exclude
    public double distance

    ...
}
文档快照
读取对象后,可以设置此字段(仅客户端):

AnuncioPrincipal anuncioPrincipal1 = doc.getDocument().toObject(AnuncioPrincipal.class);
anuncioPrincipal1.distance = ...

然后您可以使用这个新的
distance
属性对视图中显示的列表中的项目进行排序,例如:。

思考了几个小时后,我决定使用以下逻辑:

query.addSnapshotListener(new EventListener<QuerySnapshot>() {
@Override
public void onEvent(@Nullable QuerySnapshot documentSnapshots, @Nullable FirebaseFirestoreException e) {
for (DocumentChange doc : documentSnapshots.getDocumentChanges()){
if (doc.getType() == DocumentChange.Type.ADDED){
AnuncioPrincipal anuncioPrincipal1 = doc.getDocument().toObject(AnuncioPrincipal.class);
Location localAnuncioAtual = new Location("ProviderAun");

                                                    localAnuncioAtual.setLatitude(anuncioPrincipal1.getLatAnun());
                                                    localAnuncioAtual.setLongitude(anuncioPrincipal1.getLongAnun());
             anuncioPrincipal1.getCidadeAnunciante());
double distancia1 = Math.round(localizacaoUser.distanceTo(localAnuncioAtual)/1000);   // Distance in Km                                                 adapter.getItem(doc.getNewIndex()).setDistancia(distancia1); // SetDistance Between to Adapter
                                                    adapter.notifyDataSetChanged();
                                                }
                                            }
                                        }
                                    });
query.addSnapshotListener(新的EventListener(){
@凌驾
public void onEvent(@Nullable QuerySnapshot documentSnapshots,@Nullable FirebaseFirestoreException e){
对于(DocumentChange文档:documentSnapshots.getDocumentChanges()){
if(doc.getType()==DocumentChange.Type.ADDED){
AnuncioPrincipal anuncioPrincipal1=doc.getDocument().toObject(AnuncioPrincipal.class);
地点localAnuncioAtual=新地点(“ProviderAun”);
localAnuncioAtual.setLatitude(anuncioPrincipal1.getLatAnun());
localAnuncioAtual.setLongitude(anuncioPrincipal1.getLongAnun());
anuncioPrincipal1.getcidadeanuciante());
double DistanceA1=Math.round(localizacaoUser.distanceTo(LocalAnuncious)/1000);//以公里为单位的距离适配器.getItem(doc.getNewIndex()).SetDistanceA(DistanceA1);//设置到适配器的距离
adapter.notifyDataSetChanged();
}
}
}
});

为了用本地数据更新RecyclerView(不在Firestore数据库上托管),我为它创建了一个数据离线的新适配器(缓存),并在我的
RecyclerView.setAdapter(adapter2)上使用这个适配器
,这很好。

如果您需要不同的排序顺序,执行不同的查询乍一看对我来说似乎是明智的。您的意思是:“但这花费了我很多,而且不是很有效”你能解释一下你的意思吗?我们按咨询付费,所以如果我按州进行咨询…这可能会导致很多记录,因为每个用户与每个对象都有一定距离…想象一下1000个用户与10个对象的情况…你可能想验证这个假设。如果你的文档没有更改,它们将被读取RO本地缓存,并且每次读取都不会收费。如果文档确实发生了变化,您应该从数据库服务器重新读取它们,以确保您拥有最新的数据。在后一种情况下,您可能需要考虑使用FielBaseRealTimeDATABASE,而当您有许多小的写入操作时,这将更具成本效益。今天,为了实现您在下面回答的内容,我使用Firebase Recycler Firestore,对于这种类型的数据,您不会将列表传递给Recycler,而是传递给Options对象,例如:
Options=new FirestoreRecyclerOptions.Builder().setQuery(query,AnuncioPrincipal.class).build();adapter.updateOptions(Options)
即使使用选项进行更新,它仍然有如何更改?我需要更新Firestore中排除的字段(距离)每次更改值时,您都必须重新排列列表中的项目。然后,您的适配器/视图将根据新值进行更新。这就是问题所在,当我们使用Firestore Recycler Adapt时,列表不可用且无法访问