为什么要两次进入观察者?

时间:2020-04-02 14:32:23

标签: android mvvm

我在每个片段中都有一个观察者,但我遇到的问题是它两次进入观察者,而且我不太了解,因为,让我们看看是否可以帮助我。这是我的用例:

class GetRegionsCasesUseCase {

    fun getRegionCases(casesRepositoryLiveDataContract: CasesRepositoryLiveDataContract) {
        object: AsyncTask<List<CasesModel>, Void, Void>(){
            override fun doInBackground(vararg params: List<CasesModel>?): Void? {
                try {
                    val rows: List<List<String>> = csvReader().readAll(URL(CASES_FILE_URL).openStream())
                    val listCCAA: List<CasesModel> = rows.map {
                        CasesModel(it[0], it[1], it[2], it[3], it[4], it[5])
                    }
                    val lastDate: String = listCCAA.map { it.date }.last { it.isNotEmpty() }
                    val finalItems = listCCAA.filter { lastDate == it.date }
                    casesRepositoryLiveDataContract.onSuccess(finalItems)
                } catch (e: Exception) {
                    e.printStackTrace()
                }
                return null
            }
        }.execute()
    }
}

这是我的存储库类:

class CasesFragmentRepository {

    @Inject
    lateinit var getRegionsCasesUseCase: GetRegionsCasesUseCase

    private val regionsCases = MutableLiveData<List<CasesModel>>()

    init {
        GlobalApplication.useCaseApplicationComponent?.inject(this)
    }

    fun getRegionsCasesListFromRepository(): LiveData<List<CasesModel>> {
        return regionsCases
    }

    fun getRegionsCasesListUseCase() {
        getRegionsCasesUseCase.getRegionCases(object : CasesRepositoryLiveDataContract {
            override fun showError(show: Boolean?) {

            }

            override fun onLoading(loading: Boolean) {

            }

            override fun onSuccess(regionsCasesList: List<CasesModel>?) {
                regionsCases.postValue(regionsCasesList)
            }
        })
    }
}

这是我的视图模型:

class CasesFragmentViewModel: ViewModel(), CasesFragmentContract {

    @Inject
    lateinit var casesFragmentRepository: CasesFragmentRepository

    init {
        GlobalApplication.viewModelApplicationComponent?.inject(this)
    }

    fun getCountriesFlagLiveData(): LiveData<List<CasesModel>> {
        casesFragmentRepository.getRegionsCasesListUseCase()
        return casesFragmentRepository.getRegionsCasesListFromRepository()
    }

    override fun getRegionsCasesFromFile() {
        casesFragmentRepository.getRegionsCasesListFromRepository()
    }
}

最后,这是我的片段:

class CasesFragment : Fragment() {

    private lateinit var casesFragmentViewModel: CasesFragmentViewModel

    companion object {
        fun newInstance(): CasesFragment {
            return CasesFragment()
        }
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.fragment_cases, container, false)
    }

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

        casesFragmentViewModel = ViewModelProvider(this).get(CasesFragmentViewModel::class.java)

        casesFragmentViewModel.getCountriesFlagLiveData().observe(viewLifecycleOwner, Observer {
            Toast.makeText(context, it[0].casos, Toast.LENGTH_SHORT).show()
        })
    }
}

我添加了一个Toast,看看它是否向我显示了数据,但确实连续显示两次。我不知道为什么会这样。

0 个答案:

没有答案