Android LiveData在没有观察者的情况下更新数据
我正在编写一个培训预算应用程序,它使用LiveData、数据绑定、Room、MVVM和CLEAN 现在我面临着一个奇怪的问题。当前(应用程序全新启动)我的应用程序看起来像一个带有浮动操作按钮的空屏幕 单击浮动操作按钮将创建新的MonthBalanceDatabase数据类,但前提是数据库中已经并没有当前月份Android LiveData在没有观察者的情况下更新数据,android,kotlin,android-room,android-livedata,Android,Kotlin,Android Room,Android Livedata,我正在编写一个培训预算应用程序,它使用LiveData、数据绑定、Room、MVVM和CLEAN 现在我面临着一个奇怪的问题。当前(应用程序全新启动)我的应用程序看起来像一个带有浮动操作按钮的空屏幕 单击浮动操作按钮将创建新的MonthBalanceDatabase数据类,但前提是数据库中已经并没有当前月份 @Entity(tableName = "month_balance_overview") data class MonthBalanceDatabase( @Pr
@Entity(tableName = "month_balance_overview")
data class MonthBalanceDatabase(
@PrimaryKey(autoGenerate = true)
var id: Int = 0,
var year: String, //ex 2020
var month: String, //ex SEPTEMBER
var income: Long = 0,
var consumption: Long = 0,
var balance: Long = 0
)
刀
这就是问题所在。im使用getCurrentMonth方法进行进一步检查。应用程序是否应该创建新的月份
//this is part of my viewModel
private val _currentMonth: LiveData = budgetRepository.currentMonth
val currentMonth: LiveData
get() = _currentMonth
/**
* Method called via dataBinding on floating button click
*/
fun addNewMonthIfPossible() {
if (currentMonth.value == null) {
scope.launch(Dispatchers.IO) {
//creating current month with year == 2020 and month == SEPTEMBER
//and its working good
budgetRepository.createCurrentMonth(getCurrentYear(), getCurrentMonth())
}
}
}
但是,如果我的片段中没有观察者,那么currentMonth将永远不会更新,这将导致我的视图中出现多个类似的月份
因此,当currentMonth上的观察者被注释包围时,我的LiveData将永远不会更新,并且同一个月的多个实例将在浮动按钮单击时添加到DB中。为什么它是这样工作的?有没有一种不用设置观察者的方法来处理它?或者我做错了什么
//this is part of my viewModel
private val _currentMonth: LiveData = budgetRepository.currentMonth
val currentMonth: LiveData
get() = _currentMonth
/**
* Method called via dataBinding on floating button click
*/
fun addNewMonthIfPossible() {
if (currentMonth.value == null) {
scope.launch(Dispatchers.IO) {
//creating current month with year == 2020 and month == SEPTEMBER
//and its working good
budgetRepository.createCurrentMonth(getCurrentYear(), getCurrentMonth())
}
}
}
class MonthOverviewFragment : Fragment() {
lateinit var viewModel: MonthViewModel
lateinit var viewModelFactory: MonthViewModelFactory
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val binding: MonthOverviewFragmentBinding = DataBindingUtil.inflate(
inflater,
R.layout.month_overview_fragment, container, false
)
val application = requireNotNull(this.activity).application
val dataSource = BudgetDatabase.getInstance(application)
viewModelFactory = MonthViewModelFactory(dataSource, application)
viewModel = ViewModelProvider(this, viewModelFactory).get(MonthViewModel::class.java)
binding.monthViewModel = viewModel
binding.lifecycleOwner = viewLifecycleOwner
val adapter = MonthsAdapter()
binding.monthList.adapter = adapter
viewModel.existingMonths.observe(viewLifecycleOwner, Observer {
it?.let {
adapter.submitList(it)
}
})
//ive noticed that if this line is commented my LiveData will never update
// but i don't need to observe this variable
//viewModel.currentMonth.observe(viewLifecycleOwner, Observer {})
return binding.root
}
}