使用其他可观察到的数据

时间:2020-03-28 18:54:21

标签: rx-java rx-kotlin

我无法进入ReactiveX的心态,或者我正在使用的代码库编写得不好。

假设我有一些Observable A(),并且需要来自另一个Observable B()的数据以便对通过A的数据进行验证,那么我该如何在RxJava中完成此操作(宁愿使用RxKotlin实现)。请注意,A和B均返回对象列表的Single。

<div class="team-area">
		<div class="team-container">


			<a href="#" class="tile">
				<span class="single-img img-one">
					<span class="img-text">
						<div class="img-text-upper-container">
							<div class="img-text-title">
								<h4>John Doe</h4>
							</div>

							<div class="img-text-price">
								<h4 class="price">Lorem</h4>
								<h4 class="date">10 days ago</h4>
							</div>
						</div>
						
						<div class="img-description">
							<p>Lorem Ipsum dolor sit amet, consetssssgas...</p>
						</div>
						
						<div class="img-options-inner">
							<div class="img-options reviews">
								<h5>4.5 <i class="far fa-star"></i></h5>
							</div>


							<div class="img-options comments">
								<h5>10 <i class="far fa-comments"></i></h5>
							</div>

						</div>
						
					</span>
				</span>
			</a>



			<a href="#" class="tile">
				<span class="single-img img-two">
					<span class="img-text">
						<div class="img-text-upper-container">
							<div class="img-text-title">
								<h4>Jane Doe</h4>
							</div>

							<div class="img-text-price">
								<h4 class="price">Lorem</h4>
								<h4 class="date">10 days ago</h4>
							</div>
						</div>
						
						<div class="img-description">
							<p>Lorem Ipsum dolor sit amet, consetssssgas...</p>
						</div>

						<div class="img-options-inner">
							<div class="img-options reviews">
								<h5>4.5 <i class="far fa-star"></i></h5>
							</div>


							<div class="img-options comments">
								<h5>10 <i class="far fa-comments"></i></h5>
							</div>

						</div>
					</span>
				</span>
			</a>



			<a href="#" class="tile">
				<span class="single-img img-three">
					<span class="img-text">
						<div class="img-text-upper-container">
							<div class="img-text-title">
								<h4>Baby Doe</h4>
							</div>

							<div class="img-text-price">
								<h4 class="price">Lorem</h4>
								<h4 class="date">10 days ago</h4>
							</div>
						</div>
						
						<div class="img-description">
							<p>Lorem Ipsum dolor sit amet, consetssssgas...</p>
						</div>
						<div class="img-options-inner">
							<div class="img-options reviews">
								<h5>4.5 <i class="far fa-star"></i></h5>
							</div>


							<div class="img-options comments">
								<h5>10 <i class="far fa-comments"></i></h5>
							</div>

						</div>
					</span>
				</span>
			</a>



			<a href="#" class="tile">
				<span class="single-img img-four">
					<span class="img-text">
						<div class="img-text-upper-container">
							<div class="img-text-title">
								<h4>Mr. Doe</h4>
							</div>

							<div class="img-text-price">
								<h4 class="price">Lorem</h4>
								<h4 class="date">10 days ago</h4>
							</div>
						</div>
						
						<div class="img-description">
							<p>Lorem Ipsum dolor sit amet, consetssssgas...</p>
						</div>
						<div class="img-options-inner">
							<div class="img-options reviews">
								<h5>4.5 <i class="far fa-star"></i></h5>
							</div>


							<div class="img-options comments">
								<h5>10 <i class="far fa-comments"></i></h5>
							</div>

						</div>
					</span>
				</span>
			</a>

		</div>
	</div>

更新1:应强调验证需要多个来源,因此您可以拥有B,C,D等。

1 个答案:

答案 0 :(得分:0)

因此您可以使用.map {}运算符:

fun A() : Single<List<Foo>> {
    return B.getAll().map { allFromB ->  
        val record = readRecords()
        if (allFromB.contains(record)) {
           // ... some validation
        }
        ...
    }
}

更新 如果您几乎没有这样的可观测对象,则需要使用Observables.combineLatest()Observable.combineLatest()(取决于您使用的RX版本):

fun B(): Single<List<BarB>> {
   ...
}

fun C(): Single<List<BarC>> {
   ...
}

fun D(): Single<List<BarD>> {
   ...
}

fun A() : Single<List<Foo>> {
    return Observable.combineLatest(B.getAll(), C.getAll(), D.getAll) { barB, barC, barD -> 
            val record = readRecords()
            //Do your staff here with all this vals and return List<Foo>
        }
}