I am trying to manipulate the data pulled from an API using retrofit and LiveData. Below is my code
viewModel.getTransactions("withdrawals").observe(this, Observer {
if (it.getError() == null) {
dataAdapter = ArrayList(it.getTransaction()?.data)
if (dataAdapter.size == 0) {
// no withdrawal
withdrawSum = 0
} else {
it.getTransaction()?.data?.forEachIndexed { _, element ->
withdrawSum += Math.abs(element.attributes.amount)
}
}
} else {
// Error
}
})
viewModel.getTransactions("deposits").observe(this, Observer {
if(it.getError() == null){
dataAdapter = ArrayList(it.getTransaction()?.data)
if(dataAdapter.size == 0){
// no deposit
depositSum = 0
}else {
it.getTransaction()?.data?.forEachIndexed{ _, element ->
depositSum+=Math.abs(element.attributes.amount)
}
}
} else {
// Error
}
})
// difference will be 0 since deposit = 0 and withdrawal = 0
difference = deposit - withdrawal
What I am having issue is with this line difference = deposit - withdrawal
. This is called immediately as opposed to waiting for Retrofit calls to complete before doing subtraction. What can i do to solve this issue? A horrible solution would be to nest the deposit code inside withdrawal's it.getError() == null
, is there a cleaner solution?