将Observable List更改为Hashmap Singleton

时间:2016-10-12 07:35:23

标签: android rx-java retrofit2

我有一个单例来缓存对象,从这里我从List创建一个observable,这个List是来自API的响应,它充满了对象。 (JSON)

 private static BehaviorSubject<List<Model>> observableModelsList;
    private static Observable<List<Model>> observable = myAPI.loadModelsRx();
    private static Subscription subscription;
    private PoiSingleton() {
    }

    public static PoiSingleton getInstance() {
        return ourInstance;
    }

    public static void resetObservable() {
        observablePoisList = BehaviorSubject.create();

        if (subscription != null && !subscription.isUnsubscribed()) {
            subscription.unsubscribe();
        }
        subscription = observable.subscribe(new Subscriber<List<Model>>() {
            @Override
            public void onCompleted() {
                // do nothing
            }

            @Override
            public void onError(Throwable e) {
                observablePoisList.onError(e);
            }

            @Override
            public void onNext(List<Model> models) {
                observablePoisList.onNext(models);
            }
        });
    }


    public static Observable<List<Poi>> getPoisObservable() {
        if (observablePoisList == null) {
            resetObservable();
        }
        return observablePoisList;
    }

我想要实现的是从List创建一个HashMap,键应​​该是对象的ID和对象本身的值。

我是Android和Retrofit / RxJava的新手,所以在什么方法/阶段负责创建HashMap?

0 个答案:

没有答案