【问题标题】:PendingResult<PlaceBuffer> setResultCallback DelayPendingResult<PlaceBuffer> setResultCallback 延迟
【发布时间】:2018-10-19 02:44:38
【问题描述】:

我创建了一个应用程序,其中一个地点列表保存在数据库中。我只根据 Google 文档保存了已保存地点的地点 ID。

然后我创建了一个界面供用户输入他们当前的位置 (mCurrentLocation)。

然后我创建了一个 Asynctask 来遍历数据库中保存的每个地点 ID,并检查哪些地点在用户位置 500 米范围内。这是在 doInBackground 中完成的。根据 Google 文档,我使用了 getPlaceById 和 setResultCallback

在 postExecute 中,Recycler 视图被更新。

问题:PendingResult setResultCallback 似乎有延迟。根据我的日志,在 setResultCallback 尚未完成时已经调用了 PostExecute!结果,我的 Recycler 视图没有看到所有结果

日志:

for循环内部

for循环内部

for循环内部

for 循环退出

onPostExecute 进入

for循环内部

for循环内部

@覆盖 受保护的列表 doInBackground(列表...列表) {

        ArrayList<Places> selectedPlaces = new ArrayList<>();

        for ( int i = 0; i < lists[0].size(); i++) {


            // Get the place from the placeID
            PendingResult<PlaceBuffer> placeResult = Places.GeoDataApi.
                    getPlaceById(mGoogleApiClient, lists[0].get(i).getAddress());

            placeResult.setResultCallback(new ResultCallback<PlaceBuffer>() {
                @Override
                public void onResult(@NonNull PlaceBuffer places) {

                    Log.v(LOG_TAG, "Inside for loop");

                    // Get the latitude and longitude for the specific place
                    LatLng latLng = places.get(0).getLatLng();

                    // Set the location object for the specific place
                    Location A = new Location("Place");
                    A.setLatitude(latLng.latitude);
                    A.setLongitude(latLng.longitude);

                    // get the distance of the place from the selected location
                    float distance = A.distanceTo(mUserLocation);

                    // if the distance is less than 500m
                    if (distance < 500) {
                        selectedPlaces.add(lists[0].get(position));
                    }
                }
            });
        }

        Log.v(LOG_TAG, "for loop exited");
        return selectedPlaces;
    }

    @Override
    protected void onPostExecute(List<Places> places) {
        Log.v(LOG_TAG, "onPostExecute entered");
        //update Recycler view

    }

【问题讨论】:

    标签: android google-api google-places-api android-pendingintent


    【解决方案1】:

    我修改了代码。我应该使用 await() 而不是使用 setResultCallback。

    所以新代码应该是这样的:

    PlaceBuffer places = placeResult.await();
    

    然后删除

    placeResult.setResultCallback(new ResultCallback<PlaceBuffer>() {
                    @Override
                    public void onResult(@NonNull PlaceBuffer places) {
    

    【讨论】:

      猜你喜欢
      • 2017-07-28
      • 1970-01-01
      • 2019-06-07
      • 2015-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-10
      相关资源
      最近更新 更多