在片段上运行多生命周期

发布于 2025-01-25 13:27:34 字数 2082 浏览 3 评论 0原文

我有一个家庭片段,在该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)
    }
}

在尝试运行该应用程序时,它将在此错误消息中崩溃。

您可以在此链接上找到存储库,并在HomefragmentMainActivity上注意 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.
enter image description here

You can find the repository on this link, and pay attention on HomeFragment and MainActivity
https://github.com/gandarain/money_track_app

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜空下最亮的亮点 2025-02-01 13:27:34

我怀疑函数calculateIncome()不是暂停,并且它阻止了当前线程(主线程)。
标记计算函数为cashflowdao中应解决问题。它不会阻止主线程。

cashflowdao类:

suspend fun calculateIncome(...): Int

I suspect the function calculateIncome() is not suspend, and it is blocking the current thread (Main Thread).
Marking calculateIncome() function as suspend in CashFlowDao should solve the problem. It will not block the Main Thread.

In CashFlowDao class:

suspend fun calculateIncome(...): Int
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文