我正在尝试创建一个每秒发出多个对象的ConnectableFlowable。以下代码发出128个项目,然后停止。再也不会发射数据了。我试图删除flatMap()调用,因为它会无限发出十个项目。有什么建议吗?
private ConnectableFlowable<PlcDataPackage> createConnectablePlcFlowable() {
return createIntervalPlcFlowable().onBackpressureLatest().publish();
}
private Flowable<PlcDataPackage> createIntervalPlcFlowable() {
return Flowable.interval(1, TimeUnit.SECONDS)
.onBackpressureLatest()
.flatMap((Function<Long, Publisher<PlcDataPackage>>) aLong ->
mDataPackageFlowable);
}
private Flowable<PlcDataPackage> createPlcFlowable() {
return Flwable.create(...,BackpressureStrategy.LATEST)
.subscribeOn(Schedulers.single())
}
mDataPackageFlowable = createPlcFlowable();
mConnectableFlowable = createConnectablePlcFlowable();
mConnectableFlowable.connect();
订阅代码:
addDisposable(mGetPlcUpdatesChanelUseCase.execute()
.observeOn(AndroidSchedulers.mainThread())
.subscribe(plcDto -> Timber.d(String.valueOf(++dataCount)), Timber::e));
UseCase代码:
public class GetPlcUpdatesChanelUseCase extends UseCase<PlcDto, Object> {
private final PlcRepository mPlcRepository;
public GetPlcUpdatesChanelUseCase(PlcRepository plcRepository) {
mPlcRepository = plcRepository;
}
@Override
public Flowable<PlcDto> buildFlowable(Optional<Object> optional) {
return mPlcRepository.getUpdatesChannel();
}
@Override
public boolean isParamsRequired() {
return false;
}
}
存储库
public final class PlcRepositoryImpl implements PlcRepository {
private final PlcCore mPlcCore;
private final PlcInfoTopPlcDtoTransformer mPlcInfoTopPlcDtoTransformer = new PlcInfoTopPlcDtoTransformer();
public PlcRepositoryImpl(PlcCore plcCore) {
mPlcCore = plcCore;
}
@Override
public Flowable<PlcDto> getUpdatesChannel() {
return mPlcCore.getPlcConnectableFlowable()
.map(mPlcInfoTopPlcDtoTransformer::transform);
}
@Override
public Flowable<Object> setDeviceProfileDto(DeviceProfileDto deviceProfileDto) {
return mPlcCore.setDeviceProfileDto(deviceProfileDto);
}
}