Mapsforge不会从Realm重绘群集POI

时间:2016-11-25 15:39:26

标签: android realm mapsforge

我使用Realm从DB加载POI。接下来,通过mapsforge将此点添加到集群实现中。 这段代码效果很好:

 RealmResults<GasolineRealm> gasolineRealms = presenter.getRealm().where(GasolineRealm.class)
            .findAll();
    for(int i=0; i<gasolineRealms.size(); i++){
        geoItems.add(new GasolineGeoItem(gasolineRealms.get(i).getId(),
                gasolineRealms.get(i).getLocalizationRealms().get(0).getTitle(),
                gasolineRealms.get(i).getCoordinateLatitude(),
                gasolineRealms.get(i).getCoordinateLongitude()));
        clusterer.addItem(geoItems.get(i));
    }

它在地图上绘制所有POI,但如果我使用RxJava for Realm集群,则永远不要在地图上添加POI。 这是代码:

Realm.getDefaultInstance().where(GasolineRealm.class)
            .findAllAsync()
            .asObservable()
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new Observer<RealmResults<GasolineRealm>>() {
                @Override
                public void onCompleted() {

                }

                @Override
                public void onError(Throwable e) {

                }

                @Override
                public void onNext(RealmResults<GasolineRealm> gasolineRealms) {
                    for(int i=0; i<gasolineRealms.size(); i++){
                        geoItems.add(new GasolineGeoItem(gasolineRealms.get(i).getId(),
                                gasolineRealms.get(i).getLocalizationRealms().get(0).getTitle(),
                                gasolineRealms.get(i).getCoordinateLatitude(),
                                gasolineRealms.get(i).getCoordinateLongitude()));
                        clusterer.addItem(geoItems.get(i));
                    }
                }
            });

2 个答案:

答案 0 :(得分:1)

如果你这样做,那两个就差不多了

     Subscription subscription = presenter.getRealm().where(GasolineRealm.class)
        .findAllAsync()
        .asObservable()
        .filter(RealmResults::isLoaded)
        .first()
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(new Observer<RealmResults<GasolineRealm>>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(RealmResults<GasolineRealm> gasolineRealms) {
                for(int i=0; i<gasolineRealms.size(); i++){
                    GasolineRealm gasolineRealm = gasolineRealms.get(i);
                    geoItems.add(new GasolineGeoItem(gasolineRealm.getId(),
                            gasolineRealm.getLocalizationRealms().get(0).getTitle(),
                            gasolineRealm.getCoordinateLatitude(),
                            gasolineRealm.getCoordinateLongitude()));
                    clusterer.addItem(geoItems.get(i));
                }
            }
        });

答案 1 :(得分:0)

我的项目中的两个示例都适用于标准视图...

我建议尝试以下列方式实现:

Subscription s = Observable.create(new Observable.OnSubscribe<List<GasolineRealm>>() {
        @Override
        public void call(Subscriber<? super List<GasolineRealm>> subscriber) {
            Realm realm = presenter.getRealm();
            RealmResults<GasolineRealm> gasolineRealms = realm.where(GasolineRealm.class).findAll();
            subscriber.onNext(realm.copyFromRealm(gasolineRealms));
            realm.close();
            subscriber.onCompleted();
        }
    }).subscribe(new Subscriber<List<GasolineRealm>>() {
        @Override
        public void onCompleted() {
            Log.i(TAG, "GasolineRealm Subscriber is completed");
        }

        @Override
        public void onError(Throwable throwable) {
            Log.e(TAG, "GasolineRealm Subscriber onError " + throwable.getMessage());
        }

        @Override
        public void onNext(List<GasolineRealm> gasolineRealms) {
            for (int i = 0; i < gasolineRealms.size(); i++) {
                geoItems.add(new GasolineGeoItem(gasolineRealms.get(i).getId(),
                        gasolineRealms.get(i).getLocalizationRealms().get(0).getTitle(),
                        gasolineRealms.get(i).getCoordinateLatitude(),
                        gasolineRealms.get(i).getCoordinateLongitude()));
                clusterer.addItem(geoItems.get(i));
            }
        }
    });