和改造多次调用另一个请求中的请求

和改造多次调用另一个请求中的请求

本文介绍了使用 rxJava 和改造多次调用另一个请求中的请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 MVVM 和 rxJava 并进行改造来发送我的请求.我有一个底部导航视图,其中有 5 个片段,在其中一个片段中,我必须发送一个请求,然后发送响应,我必须向我的服务器发送另一个请求.这是我的 ViewModel 类:

I'm using MVVM and rxJava and retrofit to send my request.I have a bottom navigation view which has 5 fragments and in one of them, I have to send a request and after it, the response is delivered, I have to send another request to my server.this is my ViewModel class :

class MyViewModel: ViewModel() {

val compositeDisposable = CompositeDisposable()
val myFirstReqLiveData = MutableLiveData<myFirstReqModel>()
val mySecondReqLiveData = MutableLiveData<mySecondReqModel>()

    fun getFirstReq(token:String){

    val firstReqDisposable = RetrofitClientInstance.getRetrofitInterface()
        .getFirstReq(token)
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread()).singleElement()
        .subscribe({
                it-> myFirstReqLiveData.value = it
        },{
            errorFirstReqLiveData.value = it
        },{

        })
    compositeDisposable.add(firstReqDisposable)

}

    fun getSecondReq(token:String){

    val secondReqDisposable = RetrofitClientInstance.getRetrofitInterface()
        .getSecondReq(token)
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread()).singleElement()
        .subscribe({
                it-> mySecondReqLiveData.value = it
        },{
            errorSecondReqLiveData.value = it
        },{

        })
    compositeDisposable.add(SecondReqDisposable)

}

    override fun onCleared() {
    super.onCleared()
    compositeDisposable.clear()
}

}

在我的片段中,我是这样实现的:

and in my fragment, I implement this way:

class FirstTabFragment : Fragment() {
private lateinit var myViewModel: MyViewModel

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {

myViewModel = ViewModelProviders.of(activity!!).get(MyViewModel::class.java)

        getFirstReq(myViewModel, token!!)
        observeFirstReq(myViewModel)
        observeFirstReqError(myViewModel)

        observeSecondReq(myViewModel)
        observeSecondReqError(myViewModel)
}

    fun getFirstReq(viewModel: MyViewModel, token: String) {
    viewModel.getFirstReq(token)
}

   fun observeFirstReq(viewModel: MyViewModel) {
    viewModel.getFirstReqLiveData().observe(this, Observer { myFirstReqModel ->
   getSecondReq(myViewModel)
    }
   }

   fun getSecondReq(viewModel: MyViewModel, token: String) {
    viewModel.getSecondReq(token)
   }


    fun observeSecondReq(viewModel: MyViewModel) {
    viewModel.getSecondReqLiveData().observe(this, Observer { mySecondReqModel ->
   //do some work with my data
    }
   }

我的问题是当我切换标签时,我的第二个请求被调用了几次.

my problem is when I switch my tabs, my second request called several times.

我想我每次重新打开我的片段时都会分配一个新的订阅,所以它会调用几次.

I think I assign a new subscribe every time i reopen my fragment, so it called several times.

我该如何解决这个问题?!

how can I fix this issue?!

推荐答案

创建下面的类

open class Event<out T>(private val content: T) {

    var hasBeenHandled = false
        private set // Allow external read but not write

    /**
     * Returns the content and prevents its use again.
     */
    fun getContentIfNotHandled(): T? {
        return if (hasBeenHandled) {
            null
        } else {
            hasBeenHandled = true
            content
        }
    }

    /**
     * Returns the content, even if it's already been handled.
     */
    fun peekContent(): T = content
}

在 Viewmodel 中像这样更改

in Viewmodel change like this

val myFirstReqLiveData = MutableLiveData<Event<myFirstReqModel>>()
val mySecondReqLiveData = MutableLiveData<Event<mySecondReqModel>>()

在片段类中

fun observeFirstReq(viewModel: MyViewModel) {
    viewModel.getFirstReqLiveData().observe(this, EventObserver { myFirstReqModel ->
   getSecondReq(myViewModel)
    }
   }

改变

it-> myFirstReqLiveData.value = it to
it-> myFirstReqLiveData.value = Event(it)

尝试使用这种方式,如果这对您有帮助.

try using this way, if this helps you.

这篇关于使用 rxJava 和改造多次调用另一个请求中的请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 10:54