【问题标题】:Calculation Problem in Helper Class - Return is hit before calculation助手类中的计算问题 - 在计算之前返回
【发布时间】:2019-04-01 00:59:39
【问题描述】:

我目前的问题是我想创建一个 Helper 类来计算我的成本(存储在数据库中),以便更好地组织一切(因为我需要在我的两个活动中进行此计算)。

现在计算代码(工作正常)只是写在两个活动中(这使得一切都变得相当丑陋)。

所以:我创建了一个“CostIntervalHelper”-Class。

我把我的代码放在那里并改变了一些东西,但我现在遇到了一个大问题。为了更好地解释它,首先这里是计算方法的代码:

 public BigDecimal calculateMonthlyCosts(SubViewModel subViewModel, Context context){
    //set this two times just for debugging reasons to see if the calculation actually works
    cMonthly = new BigDecimal(0);

    subViewModel.getAllMonthlyCosts().observe((LifecycleOwner) context, new Observer<BigDecimal>() {
        @Override
        public void onChanged(@Nullable BigDecimal bigDecimal) {
            cMonthly = new BigDecimal(0);
            if (bigDecimal != null) {
                cMonthly = cMonthly.add(bigDecimal);
            }
        }
    });

    subViewModel.getAllBiannuallyCosts().observe((LifecycleOwner) context, new Observer<BigDecimal>() {
        @Override
        public void onChanged(@Nullable BigDecimal bigDecimal) {
            cBiannually = new BigDecimal(0);
            if (bigDecimal != null) {
                cBiannually = cBiannually.add(bigDecimal.divide(new BigDecimal(6), 2));
            }
        }
    });

    subViewModel.getAllYearlyCosts().observe((LifecycleOwner) context, new Observer<BigDecimal>() {
        @Override
        public void onChanged(@Nullable BigDecimal bigDecimal) {
            cYearly = new BigDecimal(0);
            if (bigDecimal != null) {
                cYearly = cYearly.add(bigDecimal.divide(new BigDecimal(12), 2));
                cMonthly = cMonthly.add(cBiannually).add(cYearly);
            }
        }
    });

    //return is hit before the calculation so it only returns "0"
    return cMonthly;
}

现在我们来解决问题(已经在代码中放入了一些 cmets):

每当我调用该方法时,都会首先调用返回 cMonthly。 在调试模式下,我检查了一下,我可以说:在返回语句被命中后,计算就完成了(以正确的方式)。

现在我需要一个想法或示例,如何让我的代码首先(以正确的顺序)执行所有 subViewModel.getAll(Monthly/Biannually/Yearly)Costs().[...] 方法,然后点击返回语句。

我知道这有点棘手,因为我使用的是 LiveData/ViewModel,但也许有人有想法!会很棒!先谢谢了!

【问题讨论】:

    标签: java android android-livedata android-jetpack


    【解决方案1】:
    public BigDecimal calculateMonthlyCosts(SubViewModel subViewModel...) {
    

    如果您使用的是 LiveData,那么您的计算不应立即以同步函数的形式。

    您不想返回 BigDecimal,您想创建一个 LiveData,它基于您的其他 LiveData 之间的链,当任一 LiveData 发生更改时,它将更新 BigDecimal。

    为此,您应该创建一个 MediatorLiveData,并且应该观察 LiveData 以在其任何“组件”发生变化时接收 BigDecimal 的最新值。

    public LiveData<BigDecimal> calculateMonthlyCosts(SubViewModel subViewModel) {
        MediatorLiveData<BigDecimal> mediator = new MediatorLiveData<>() {
            private BigDecimal cMonthly = new BigDecimal(0);
            private BigDecimal cBiannually = new BigDecimal(0);
            private BigDecimal cYearly = new BigDecimal(0);
    
            {
                addSource(subViewModel.getAllMonthlyCosts(), new Observer<BigDecimal>() {
                    @Override
                    public void onChanged(@Nullable BigDecimal bigDecimal) {
                        if (bigDecimal != null) {
                            cMonthly = cMonthly.add(bigDecimal);
                            setValue(cMonthly);
                        }
                    }
                });
    
                addSource(subViewModel.getAllBiannuallyCosts(), new Observer<BigDecimal>() {
                    @Override
                    public void onChanged(@Nullable BigDecimal bigDecimal) {
                        if (bigDecimal != null) {
                            cBiannually = cBiannually.add(bigDecimal.divide(new BigDecimal(6), 2));
                            setValue(cMonthly); // TODO: ???
                        }
                    }
                });
    
                addSource(subViewModel.getAllYearlyCosts(), new Observer<BigDecimal>() {
                    @Override
                    public void onChanged(@Nullable BigDecimal bigDecimal) {
                        if (bigDecimal != null) {
                            cYearly = cYearly.add(bigDecimal.divide(new BigDecimal(12), 2));
                            cMonthly = cMonthly.add(cBiannually).add(cYearly);
                            setValue(cMonthly);
                        }
                    }
                });
             }
        };
    
        return mediator;
    }
    

    请注意,我不能 100% 确定您的业务逻辑,可能需要遵循 herehere 概述的“使用元组组合多个 LiveData”的方式。

    您也可以参考this video

    【讨论】:

      猜你喜欢
      • 2011-06-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-25
      • 1970-01-01
      • 2019-03-31
      • 1970-01-01
      相关资源
      最近更新 更多