且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将自定义 pojo 作为参数传递给 rxjava2 Observable 中的接口方法调用?

更新时间:2023-08-28 08:26:41

我猜您有一个 List 并且想要处理每个收到的项目以进行另一个异步操作.对于这种情况,您可以对每个结果进行平面映射.

I guess you have a List and want to process each received item for another single async operation. For this case you can flatmap each result.

flatMapIterable 意味着它将列表中的每个项目拆分为 Observable 到流中的下一个操作.flatMap 表示将对接收到的值进行操作.

flatMapIterable means that it will split each item in the list as Observable to the next Operation in your Stream. flatMap means that it will do an operation on the value received.

如果您想将结果重新组合在一起,您可以在 flatMap 之后使用 toList.

If you want to put the results back together you can use toList after flatMap.

您需要为您的平面图创建一个 Observable(操作).

You need to create an Observable (Operation) for your flatmap.

科特林:

Observable.just(data)
        .flatMapIterable { it }
        .flatMap{ moviesAPI.makeMovieFavObservable(whatEver) }
        .subscribe( ... , ... , ... )

Java(未经测试)

Observable.just(data)
            //parse each item in the list and return it as observable
            .flatMapIterable(d -> d) 
            // use each item and to another observable operation
            .flatMap(data -> Observable.just(moviesAPI.movieStuff(data)))                         
            // use each result and put it back into a list
            .toList() // put the result back to a list
            // subscribe it and log the result with Tag data, throw the error and output when completed
            .subscribe( data -> Log.d("Data", "Data received "+ data), 
                        error -> error.printStackTrace(), 
                        () -> Log.d("Completed", "Completed")
            );