Search code examples
androidrealmmapsforge

Mapsforge does not redraw cluster POI from Realm


I use Realm for loading POI from DB. Next add this points to cluster realization by mapsforge. This code works well:

 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));
    }

It draw all POI on map, but if i use RxJava for Realm cluster never add POI on map. Here is the code:

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));
                    }
                }
            });

Solution

  • The two would be nearly equivalent if you did this

         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));
                    }
                }
            });