[英]RxJava - Flowable.map not getting called after flatMap
我對rxjava中的Flowable <T> vs Observable <T>有一個奇怪的問題(io.reactivex.rxjava2 - v2.0.8)。 這里我的代碼如下所示,其中map(...)。subscribe(...)函數沒有被調用/執行。
flowable.flatMap(...return new flowable...).map(...).subscribe(...)
令人驚訝的是,如果我翻轉我的代碼以使用Observable <T>而不是Flowable <T>,則會按預期調用/執行map(...)。subscribe(...) 。 我可能會遺漏一些簡單的東西,讓我知道它可能是什么?
謝謝
`
public class Application {
public static void main(String[] args) {
// Note: Working; get the list of databases
DatabaseFlowable databases = new DatabaseFlowable(sourceClient);
// Note: Working; for each database get the list of collections in it
Flowable<Resource> resources = databases
.flatMap(db -> {
logger.info(" ==> found database {}", db.getString("name"));
return new CollectionFlowable(sourceClient, db.getString("name"));
// Note: Working; CollectionFlowable::subscribeActual works as well
});
resources
.map(resource -> {
// Note: Nothing in here gets executed
logger.info(" ====> found resource {}", resource.toString());
return resource;
})
.subscribe(m -> {
// Note: Nothing in here gets executed
logger.info(m.toString());
});
}
}
public class DatabaseFlowable extends Flowable<Document> {
private final static Logger logger = LoggerFactory.getLogger(DatabaseFlowable.class);
private final MongoClient client;
public DatabaseFlowable(MongoClient client) {
this.client = client;
}
@Override
protected void subscribeActual(Subscriber<? super Document> subscriber) {
ListDatabasesIterable<Document> cursor = client.listDatabases();
MongoCursor<Document> iterator = cursor.iterator();
while(iterator.hasNext()) {
Document item = iterator.next();
if (!item.isEmpty()) {
String message = String.format(" found database name: %s, sizeOnDisk: %s",
item.getString("name"), item.get("sizeOnDisk"));
logger.info(message);
subscriber.onNext(item);
}
}
subscriber.onComplete();
}
}
public class CollectionFlowable extends Flowable<Resource> {
private final static Logger logger = LoggerFactory.getLogger(CollectionFlowable.class);
private final MongoClient client;
private final String databaseName;
public CollectionFlowable(MongoClient client, String databaseName) {
this.databaseName = databaseName;
this.client = client;
}
@Override
protected void subscribeActual(Subscriber<? super Resource> subscriber) {
MongoDatabase database = client.getDatabase(databaseName);
ListCollectionsIterable<Document> cursor = database.listCollections();
MongoCursor<Document> iterator = cursor.iterator();
while(iterator.hasNext()) {
Document item = iterator.next();
if (!item.isEmpty()) {
logger.info(" ... found collection: {}.{}", database.getName(), item.getString("name"));
Resource resource = new Resource(databaseName,
item.getString("name"),
(Document) item.get("options"));
subscriber.onNext(resource);
}
}
subscriber.onComplete();
}
}
`
那是因為您沒有正確遵循Flowable
協議。 沒有對subscriber.onSubscribe(...)
調用,也沒有遵守subscriber.request(...)
規定的限制。
由於您的實現絕不會觀察到背壓,因此要么使用Flowable.create
來創建緩沖版本,要么將實現移動到Observable
,而不是背壓。
您看到的行為的原因是下游觀察者沒有請求任何項目,因此您對onNext
的調用將被丟棄。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.