在片段上运行多生命周期
我有一个家庭片段,在该increateview
片段中,我有2个功能,可以执行lifecyclescope
。
第一个lifecyclescope
将使用cashflow
的所有列表,第二个lifecyclescope
将使用用于获取所有总计结果
和<代码>收入。
这是onCreateview
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val content = inflater.inflate(R.layout.fragment_home, container, false) as ConstraintLayout
btnNewOutcome(content)
btnNewIncome(content)
val cashFlowDao = (activity?.applicationContext as CashFlowApp).db.cashFlowDao()
loadCashFlow(cashFlowDao, content)
loadTotalTransaction(cashFlowDao, content)
return content
}
这是第一个lifecyclescope
,它将用于获取所有cashflow
历史记录,
private fun loadCashFlow(cashFlowDao: CashFlowDao, content: View) {
lifecycleScope.launch {
cashFlowDao.fetchAllCashFlow().collect {
val cashFlowList = ArrayList(it)
setupHistoryCashFlow(cashFlowList, cashFlowDao, content)
}
}
}
这是第二个lifecycyclescope 将用于获取
cashFlow
的总计,
private fun loadTotalTransaction(cashFlowDao: CashFlowDao, content: View) {
lifecycleScope.launch {
val totalIncome: Int = cashFlowDao.calculateIncome(Constant.INCOME)
val totalOutcome: Int = cashFlowDao.calculateIncome(Constant.OUTCOME)
Log.e("Total", totalIncome.toString())
setupTotalTransaction(totalIncome, totalOutcome, content)
}
}
您可以在此链接上找到存储库,并在Homefragment
和MainActivity上注意
https://github.com/gandarain/gandarain/money_track_app_app
I have a Home Fragment, and inside thatonCreateView
fragment, I have 2 function, that will execute lifecycleScope
.
The first lifecycleScope
will used to get all the list of cashFlow
, and the second lifecycleScope
will use to get all the total of outcome
and income
.
Here is the onCreateView
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val content = inflater.inflate(R.layout.fragment_home, container, false) as ConstraintLayout
btnNewOutcome(content)
btnNewIncome(content)
val cashFlowDao = (activity?.applicationContext as CashFlowApp).db.cashFlowDao()
loadCashFlow(cashFlowDao, content)
loadTotalTransaction(cashFlowDao, content)
return content
}
This is the first lifecycleScope
that will used to get all the cashFlow
history,
private fun loadCashFlow(cashFlowDao: CashFlowDao, content: View) {
lifecycleScope.launch {
cashFlowDao.fetchAllCashFlow().collect {
val cashFlowList = ArrayList(it)
setupHistoryCashFlow(cashFlowList, cashFlowDao, content)
}
}
}
This is the second lifecycleScope
that will used to get the total of cashFlow
,
private fun loadTotalTransaction(cashFlowDao: CashFlowDao, content: View) {
lifecycleScope.launch {
val totalIncome: Int = cashFlowDao.calculateIncome(Constant.INCOME)
val totalOutcome: Int = cashFlowDao.calculateIncome(Constant.OUTCOME)
Log.e("Total", totalIncome.toString())
setupTotalTransaction(totalIncome, totalOutcome, content)
}
}
When trying to run the application, it will crash with this error message.
You can find the repository on this link, and pay attention on HomeFragment
and MainActivity
https://github.com/gandarain/money_track_app
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我怀疑函数
calculateIncome()
不是暂停
,并且它阻止了当前线程(主线程)。标记
计算
函数为在
cashflowdao
中应解决问题。它不会阻止主线程。在
cashflowdao
类:I suspect the function
calculateIncome()
is notsuspend
, and it is blocking the current thread (Main Thread).Marking
calculateIncome()
function assuspend
inCashFlowDao
should solve the problem. It will not block the Main Thread.In
CashFlowDao
class: