Wie mache ich einen rekursiven, beobachtbaren Aufruf in RxJava?

Ich bin ziemlich neu bei RxJava (und das reaktive Paradigma im Allgemeinen), bitte nehmen Sie Kontakt mit mir auf.

ngenommen, ich habe diesesNews und dieses verschachtelteComment Datenstruktur

public class News {
  public int id;
  public int[] commentIds; //only top level comments
  public News(int id, int[] commentIds) {
    this.id = id;
    this.commentIds = commentIds;
  }
}

public class Comment {
  public int id;
  public int parentId; //ID of parent News or parent comment
  public int[] childIds;
  public Comment(int id, int parentId, int[] childIds) {
    this.id = id;
    this.parentId = parentId;
    this.childIds = childIds;
  }
}

und nehme an, ich habe diesen API-Endpunkt:

getComments(int commentId) //return Observable<Comment> for Comment with ID commentId

Jetzt nehmen wir an:

getComments(1); //will return Comment(1, 99, [3,4])
getComments(2); //will return Comment(2, 99, [5,6])
getComments(3); //will return Comment(3, 1, [])
getComments(4); //will return Comment(4, 1, [])
getComments(5); //will return Comment(5, 2, [])
getComments(6); //will return Comment(6, 2, [])

**

Nun, wenn ich @ haNews n = News(99, [1,2]), wie bekomme ich alle seine untergeordneten Kommentare rekursiv? um Kommentare mit der ID [1,2,3,4,5,6] zu erhalten?

**

Ich habe gesucht und bin darauf gestoßen:https: //jkschneider.github.io/blog/2014/recursive-observables-with-rxjava.htm

Dies ist die Rekursionsfunktion:

public class FileRecursion {
    static Observable<File> listFiles(File f) {
        if(f.isDirectory())
            return Observable.from(f.listFiles()).flatMap(FileRecursion::listFiles);
        return Observable.just(f);
    }

    public static void main(String[] args) {
          Observable.just(new File("/Users/joschneider/Desktop"))
                  .flatMap(FileRecursion::listFiles)
                  .subscribe(f -> System.out.println(f.getAbsolutePath()));
    }
}

s zeigt ein Beispiel, wie man rekursive beobachtbare Aufrufe macht, aber die innere Funktion f.listFiles()) ist eine blockierende Operation (gibt kein weiteres Observable zurück). In meinem Fall ist die innere Funktion getComments) ist eine nicht blockierende Funktion, die andere Observables zurückgibt. Wie mache ich das

Jede Hilfe wird sehr geschätzt.

Antworten auf die Frage(2)

Ihre Antwort auf die Frage