【问题标题】:Making a network call to update UI from within an observer method in Android Kotlin在 Android Kotlin 的观察者方法中进行网络调用以更新 UI
【发布时间】:2021-07-20 10:46:30
【问题描述】:

我正在构建一个应用程序,用户可以在其中添加他们的日历可用性,并删除这些可用性。我在 Fragment 的 onViewCreated 中设置了一个观察者方法,每次用户单击我的日历日期时都会调用该方法。通过我当前的实现,代码第一次按预期工作,然后在后续调用中遇到了一些奇怪的错误(API 调用未通过/UI 未按预期更新)。我的代码如下:

        init {

            view.setOnClickListener {

                dateTimeViewModel.haveOwnTimeslotsBeenUpdated.observe(
                viewLifecycleOwner,
                Observer { isOwnTimeslotUpdated ->

                    if (isOwnTimeslotUpdated) {

                        val startOfTimeframe =
                            LocalDate.now().yearMonth.minusMonths(10).atDay(1)
                                .atStartOfDay(timezone).toEpochSecond()

                        val endOfTimeframe =
                            LocalDate.now().yearMonth.plusMonths(10).atDay(1)
                                .atStartOfDay(
                                    timezone
                                ).toEpochSecond()


                        viewLifecycleOwner.lifecycleScope.launchWhenStarted {
                            val response = dateTimeViewModel.getOwnTimeslots(
                                    startOfTimeframe,
                                    endOfTimeframe
                                )
                            val listOfTimeslots = handleResponse(response)

                            mapOfDatesAndTimeslots =
                                storeLocalDateAndTimeslotsInAMap(listOfTimeslots)

                            val dates = mapOfDatesAndTimeslots.keys

                            if(selectedDate in dates){
                                val listOfTimeslotsOnADate = mapOfDatesAndTimeslots[selectedDate]

                                val areTimeslotsWorthDisplaying = listOfTimeslotsOnADate!!.any{ timeslot ->
                                    timeslot.timeslot_status == 0 || timeslot.timeslot_status == 1 || timeslot.timeslot_status == 2
                                }

                                if(areTimeslotsWorthDisplaying){
                                    calendarDayDotView.visibility = View.VISIBLE
                                    sortTimeslotsOnAParticularDate(listOfTimeslotsOnADate)
                                } else {
                                    calendarDayDotView.visibility = View.INVISIBLE
                                    clearRecyclerViews()
                                }
                            } else {
                                calendarDayDotView.visibility = View.INVISIBLE
                                clearRecyclerViews()
                            }
                        }
                        dateTimeViewModel.setHaveOwnTimeslotsBeenUpdatedToFalse()
                    }
                }
            )

        }
    }

第一次调用后,我显示点的 UI 没有按预期工作,尽管第一次工作正常,但我的时间段排序也没有。我有一种感觉,这是因为这行代码被反复调用:viewLifecycleOwner.lifecycleScope.launchWhenStarted 但我可能是错的。我不确定从观察者方法中进行 API 调用然后将观察到的变量重置为 false 是否是最佳做法,因此请分享您的建议(如果有)。请帮助我,提前谢谢你:D

【问题讨论】:

    标签: java android kotlin


    【解决方案1】:

    你的视图模型是什么样的?

    一个危险信号会在您的点击侦听器中设置观察者,但我不确定您是否正在考虑它,因此可以查看更多代码

    【讨论】:

      猜你喜欢
      • 2016-03-04
      • 2010-12-01
      • 2021-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多