ReactiveX / RxAndroid

RxJava bindings for Android
Apache License 2.0
19.89k stars 2.94k forks source link

PublishSubject and Observable On Next working Multiple Time #407

Closed TuonBondol closed 6 years ago

TuonBondol commented 6 years ago

Hi there, I am pretty new with RxJava and Rx Android, Currently I starting to use Rx with MVVM pattern in my Android Project, I have encountered with some problem! It work fine is response success, but If it is error and go to error subject, If I try again, It will emit on Next 2 time, and If error 3 time, It will emit 3 time, Here is my code

---- In ViewModel mMainModel.requestCreateQrCode(outTradeNo = outTradeNo!!, notifyUrl = notifyUrl, totalFee = totalFee!!, sign = md5) mMainModel.onResponseQrCodeSuccess.subscribe { hideLoading() if (it.is_success == Constant.RESPONSE_SUCCESS_TRUE) { qrCodeResponse = it.response.alipay MainActivity.onReplaceFragment(mContext, ScanQrCodeFragment()) return@subscribe } onAlertError("Error Code ${it.is_success}") } mMainModel.onResponseQrCodeError.subscribe { hideLoading() onAlertError("onResponseQrCodeError $it") }

---- in Model Request private val mResponseCreateQrCodeSuccessSubject = PublishSubject.create() private val mResponseCreateQrCodeErrorSubject = PublishSubject.create<Any?>() val onResponseQrCodeSuccess: Observable = mResponseCreateQrCodeSuccessSubject val onResponseQrCodeError: Observable<Any?> = mResponseCreateQrCodeErrorSubject

fun requestCreateQrCode(outTradeNo: String, notifyUrl: String, sign: String, totalFee:String) {
    println("CountrequestCreateQrCode #######")
    WebServiceSoap.requestLive().requestQueryTransaction(outTradeNo = outTradeNo, notifyUrl = notifyUrl, sign = sign, totalFee = totalFee)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe({
                mResponseCreateQrCodeSuccessSubject.onNext(it)
            }, {
                mResponseCreateQrCodeErrorSubject.onNext(it.localizedMessage)
            })
}

JakeWharton commented 6 years ago

RxAndroid is a really tiny project which only contains an Android-specific Scheduler. It's a low-traffic project.

Your question is about RxJava in general. I would suggest either posting to StackOverflow with the 'rx-java' tag or posting to the RxJava mailing list for general usage advice and guidance. You'll reach many more people who are available and willing to help and both of those forums are much more suited for support whereas we tend to reserve issues solely for tracking bugs and feature requests.

TuonBondol commented 6 years ago

Okay! Thank you for your advise :)