每次位置更改时,我都要负责读操作吗? [英] Am I charged with read operations everytime the location is changed?

查看:81
本文介绍了每次位置更改时,我都要负责读操作吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Firestore数据库,其中包含100个位置,每个位置正确设置了latlng.我创建了一种使用回调从数据库中实际读取数据的方法:

I have a Firestore database which holds 100 locations, each with the lat and the lng correctly set. I have created a method to actually read the data from the database using a callback:

public static void readLocations(MyCallback myCallback) {
    locationsRef.get().addOnCompleteListener(task -> {
        if (task.isSuccessful()) {
            List<LocationModel> locationList = new ArrayList<>();
            for (QueryDocumentSnapshot document : task.getResult()) {
                LocationModel locationModel = document.toObject(LocationModel.class);
                locationList.add(locationModel);
            }
            myCallback.onCallback(locationList);
        }
    });
}

我正在使用LocationCallback来跟踪当前位置.使用以下代码,我在LocationCallback内调用readLocations()方法来做一些关于一些距离的数学运算:

I'm using a LocationCallback to track the current position. Using the following code, I'm calling readLocations() method inside LocationCallback to do some maths regarding some distances:

LocationCallback locationCallback = new LocationCallback() {
    @Override
    public void onLocationResult(LocationResult locationResult) {
        for (Location myLocation : locationResult.getLocations()) {
            currentLocation = myLocation;
            LatLng currentLatLng = new LatLng(currentLocation.getLatitude(), currentLocation.getLongitude());

            readLocations(locationList -> {
                for (LocationModel lm : locationList) {
                    Double lat = lm.getLat();
                    Double lng = lm.getLng();
                    Location location = new Location("");
                    location.setLatitude(lat);
                    location.setLongitude(lng);

                    //Calculations
                 }
            });
        }
    }
};

因此,每次更改当前位置时,我都会计算当前位置与数据库中位置之间的距离.因此,每次更改位置时都调用readLocations()方法,这意味着我每次都在查询数据库吗?换句话说,每次更改当前位置时,我都要进行100次读取操作?还是因为我已经使用过get()调用,将被视为单个操作?

So everytime the current location is changed, I'm calculating the distance between the current location and the locations from database. So calling readLocations() method every time the position is changed, it means that I am querying the database every time? With other words, every time the current location is changed, I'm charged with 100 read operations? Or because I already used a get() call, will be considered as a single operation?

推荐答案

您需要为每次调用get()的服务器上读取的文档数量付费.如果在客户端中启用了本地持久性(默认情况下),那么如果服务器上的文档也未更改,则文档可能来自缓存.缓存的文档读取不收费.

You are charged for the number of documents read on the server every time you call get(). If local persistence is enabled in your client (it is by default), then the documents may come from cache if the documents are also not changed on the server. Cached document reads are not charged.

这篇关于每次位置更改时,我都要负责读操作吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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