简体   繁体   English

LiveData 被多次调用

[英]LiveData getting called multiple times

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

I am posting this after much research in SO.在对 SO 进行大量研究后,我发布了这个。

I have a add button in my page.When I am clicking the add button for the first time, everything is fine.我的页面中有一个添加按钮。当我第一次单击添加按钮时,一切都很好。 But when I am clicking the add button for the next time in the same page, the add button callback livedata (saveDeviceLiveData) is getting called twice and also the service is getting called twice, for the 3rd time thrice and so on.但是,当我在同一页面中下一次单击添加按钮时,添加按钮回调 livedata (saveDeviceLiveData) 被调用两次,服务也被调用两次,第三次调用三次,依此类推。

The same case is happening for remove button too.删除按钮也发生了同样的情况。 I am not able to get what wrong I am doing.我无法理解我做错了什么。

Here is the add button code in the viewmodel:这是视图模型中的添加按钮代码:

/***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)
}

And I am observing the livedata in the fragment as:我正在观察片段中的实时数据:

/**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()
                    )
                }
            }
        }
    })

And here is the add click of the button:这是按钮的添加点击:

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())
        }
    }
}

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

I am posting this after much research in SO.在对 SO 进行大量研究后,我发布了这个。

I have a add button in my page.When I am clicking the add button for the first time, everything is fine.我的页面中有一个添加按钮。当我第一次单击添加按钮时,一切都很好。 But when I am clicking the add button for the next time in the same page, the add button callback livedata (saveDeviceLiveData) is getting called twice and also the service is getting called twice, for the 3rd time thrice and so on.但是,当我在同一页面中下一次单击添加按钮时,添加按钮回调 livedata (saveDeviceLiveData) 被调用两次,服务也被调用两次,第三次调用三次,依此类推。

The same case is happening for remove button too.删除按钮也发生了同样的情况。 I am not able to get what wrong I am doing.我无法理解我做错了什么。

Here is the add button code in the viewmodel:这是视图模型中的添加按钮代码:

/***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)
}

And I am observing the livedata in the fragment as:我正在观察片段中的实时数据:

/**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()
                    )
                }
            }
        }
    })

And here is the add click of the button:这是按钮的添加点击:

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