繁体   English   中英

LiveData 被多次调用

[英]LiveData getting called multiple times

这是屏幕快照,以便更好地理解

在对 SO 进行大量研究后,我发布了这个。

我的页面中有一个添加按钮。当我第一次单击添加按钮时,一切都很好。 但是,当我在同一页面中下一次单击添加按钮时,添加按钮回调 livedata (saveDeviceLiveData) 被调用两次,服务也被调用两次,第三次调用三次,依此类推。

删除按钮也发生了同样的情况。 我无法理解我做错了什么。

这是视图模型中的添加按钮代码:

/***Adding Device****/
private val _saveDeviceLiveData = MutableLiveData<Event<AddDeviceResponseModel>>()
val saveDeviceLiveData: LiveData<Event<AddDeviceResponseModel>>
    get() = _saveDeviceLiveData

private lateinit var addDeviceModel: LiveData<AddDeviceResponseModel>
private val addDeviceObserver = Observer<AddDeviceResponseModel> {
    _saveDeviceLiveData.value = Event(it)
}

fun onClickAddNewDevice(context: Context , isOverride: Boolean) {
    val addDeviceRequestModel = AddDeviceRequestModel(
        deviceName = URLEncoder.encode(PushNotificationUtils.getDeviceName(), "utf-8"),
        deviceType = PushNotificationUtils.getDeviceType(),
        deviceModelName = PushNotificationUtils.getDeviceModelName(context),
        osVersion = PushNotificationUtils.getOSVersion(),
        deviceToken = getFireBaseToken(),
        override = if (isOverride) "y" else "n"
    )
    addDeviceModel =
        notificationSettingsUseCase.addNewDevice(addDeviceRequestModel)//This is the service call
    if (addDeviceModel.hasObservers()) {
        addDeviceModel.removeObserver(addDeviceObserver)
    }
    addDeviceModel.observeForever(addDeviceObserver)
}

我正在观察片段中的实时数据:

/**Callback of save device***/
notificationSettingsViewModel.saveDeviceLiveData.observe(
    viewLifecycleOwner,
    Observer {
        it?.getContentIfNotHandled()?.let { responseStatus ->
            if (responseStatus.statusCode.equals(RESPONSE_STATUS_CODE)) {
                requireActivity().toastMessageOtp(responseStatus.statusMessage.toString())
                notificationSettingsViewModel.getDeviceList()
            }else{
                if(responseStatus.statusCode.equals(RESPONSE_STATUS_CODE_DEVICE_OVERRIDING)){
                    showDeviceOverridingAlert(requireContext() ,getString(R.string.alert_overriding_title) ,
                        responseStatus.errorModel?.dtoErrorMsg.toString()
                    )
                }
            }
        }
    })

这是按钮的添加点击:

fun invokeAddDeviceCall(isOverride: Boolean){
    val isNotificationEnabled = isNotificationEnabled(requireContext())
    isNotificationEnabled?.let {
        if (it) {
            //Add the device
            if ((viewModel as NotificationSettingsViewModel).getTotalDeviceCount() >= 5) {
                /***Max device reached***/
                showMaxDeviceLimitDialog(requireContext())
            } else {
                (viewModel as NotificationSettingsViewModel).onClickAddNewDevice(requireContext() , isOverride)
            }
        } else {
            enableNotificationAlert(requireContext())
        }
    }
}

这是屏幕快照,以便更好地理解

在对 SO 进行大量研究后,我发布了这个。

我的页面中有一个添加按钮。当我第一次单击添加按钮时,一切都很好。 但是,当我在同一页面中下一次单击添加按钮时,添加按钮回调 livedata (saveDeviceLiveData) 被调用两次,服务也被调用两次,第三次调用三次,依此类推。

删除按钮也发生了同样的情况。 我无法理解我做错了什么。

这是视图模型中的添加按钮代码:

/***Adding Device****/
private val _saveDeviceLiveData = MutableLiveData<Event<AddDeviceResponseModel>>()
val saveDeviceLiveData: LiveData<Event<AddDeviceResponseModel>>
    get() = _saveDeviceLiveData

private lateinit var addDeviceModel: LiveData<AddDeviceResponseModel>
private val addDeviceObserver = Observer<AddDeviceResponseModel> {
    _saveDeviceLiveData.value = Event(it)
}

fun onClickAddNewDevice(context: Context , isOverride: Boolean) {
    val addDeviceRequestModel = AddDeviceRequestModel(
        deviceName = URLEncoder.encode(PushNotificationUtils.getDeviceName(), "utf-8"),
        deviceType = PushNotificationUtils.getDeviceType(),
        deviceModelName = PushNotificationUtils.getDeviceModelName(context),
        osVersion = PushNotificationUtils.getOSVersion(),
        deviceToken = getFireBaseToken(),
        override = if (isOverride) "y" else "n"
    )
    addDeviceModel =
        notificationSettingsUseCase.addNewDevice(addDeviceRequestModel)//This is the service call
    if (addDeviceModel.hasObservers()) {
        addDeviceModel.removeObserver(addDeviceObserver)
    }
    addDeviceModel.observeForever(addDeviceObserver)
}

我正在观察片段中的实时数据:

/**Callback of save device***/
notificationSettingsViewModel.saveDeviceLiveData.observe(
    viewLifecycleOwner,
    Observer {
        it?.getContentIfNotHandled()?.let { responseStatus ->
            if (responseStatus.statusCode.equals(RESPONSE_STATUS_CODE)) {
                requireActivity().toastMessageOtp(responseStatus.statusMessage.toString())
                notificationSettingsViewModel.getDeviceList()
            }else{
                if(responseStatus.statusCode.equals(RESPONSE_STATUS_CODE_DEVICE_OVERRIDING)){
                    showDeviceOverridingAlert(requireContext() ,getString(R.string.alert_overriding_title) ,
                        responseStatus.errorModel?.dtoErrorMsg.toString()
                    )
                }
            }
        }
    })

这是按钮的添加点击:

fun invokeAddDeviceCall(isOverride: Boolean){
    val isNotificationEnabled = isNotificationEnabled(requireContext())
    isNotificationEnabled?.let {
        if (it) {
            //Add the device
            if ((viewModel as NotificationSettingsViewModel).getTotalDeviceCount() >= 5) {
                /***Max device reached***/
                showMaxDeviceLimitDialog(requireContext())
            } else {
                (viewModel as NotificationSettingsViewModel).onClickAddNewDevice(requireContext() , isOverride)
            }
        } else {
            enableNotificationAlert(requireContext())
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM