Skip to content

How to create an ObservableList? #798

@fhaust

Description

@fhaust

This is of course easy when you do it like this:

Observable<List<T>> oMap = Observable.create(...);

But this gives you no informations about added or removed entries. In effect it forces you to iterate through all entries each time the map was changed. Additionally, if the List<T> contains Observables itself, you have no way to unsubscribe from removed elements or react on their removal.

My approach is something like this:

List<T> wrapped = new ArrayList<>();

public void onInit() {
  List<Action1<T>> addedEntryObservers = new ArrayList<>();
  Observer<T> addedEntries = Observable.create(
    observer -> observer.add(t -> observer.onNext(t))
  );
}

public void add(T t) {
  wrapped.add(t);
  addedEntryObservers.forEach(o -> o.call(t));
}

But this requires me to write some strange combinators if I just want to get all entries currently in the list.

Is there a canonical way to do this (yet)?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions