Issue
I want to use a parameter of my class in an AsyncTask, but this parameter changes just after the AsyncTask. So when the AsyncTask is executed, this variable has already changed and I have the new value of the parameter instead of the old one.
I tried to use the AsyncTask.execute {}, to create a class extending AsyncTask, but nothing to do.
data class Case(var id: Long, var nom: String, var nomCourt: String, var couleur: String, var couleurTexte: String, var prix: Double, var nombre: Int = 0)
class Moteur(): Parcelable {
//I didn't past the code of the Parcelable, because it doesn't matter to this problem.
var cases : List<Case> = listOf(
Case(0, "Bouchon 1", "B1", "#008000", "#FFFFFF", 5.0),
Case(1, "Bouchon 2", "B2", "#800000", "#000000", 1.5),
Case(2, "Bouchon 3", "B3", "#000080", "#FFFFFF", 3.0),
Case(3, "Bouchon 4", "B4", "#808000", "#000000", 7.0),
Case(4, "Bouchon 5 qui est long et cher", "B5", "#008080", "#FFFFFF", 15.0)
)
enregistrerCommande() {
//Of course, the property nombre of the Cases have changed before this function is called, but here is not the problem. In the Log, I have the good values and next the wrong one.
Log.i("REGCOM", "avant : $cases")
AsyncTask.execute {
val bdd = Room.databaseBuilder(context!!, MaBdd::class.java, "historique").build()
val dao = bdd.historiqueDao()
Log.i("REGCOM", "après : ${cases[0]}")
for (i in cases) dao.insertAll(Historique(0L, i!!.nombre, false))
idProchainAchat ++
context!!.getSharedPreferences("all", Context.MODE_PRIVATE).edit().putLong("idProchainAchat", idProchainAchat).apply()
bdd.close()
}
cases.forEach{it.nombre = 0}
prixCommande = 0.0
}
}
It should register the values of the properties nombre in my db Historique, but it allways register five times 0.
Thank you for your help !
PS Sorry for my bad english, I speak french.
Solution
I've juste have found a solution, maybe not the best, but it works.
fun enregistrerCommande() {
val temp = mutableMapOf<Long, Int>()
cases.forEach { temp[it.id] = it.nombre }
doAsync {
val bdd = Room.databaseBuilder(context!!, MaBdd::class.java, "historique").build()
val dao = bdd.historiqueDao()
for ((clef, valeur) in temp) dao.insertAll(Historique(0L, clef, valeur, false))
idProchainAchat ++
context!!.getSharedPreferences("all", Context.MODE_PRIVATE).edit().putLong("idProchainAchat", idProchainAchat).apply()
bdd.close()
}
cases.forEach{it.nombre = 0}
prixCommande = 0.0
}
The solution is to "save" the datas in a Map (I used the map because I just needed the two values, but a List would be good as long as you use this method, which is not working with pointer (I think that when you write val temp = cases
you make an other list, but with the same pointers on the objects Case. I think it because of my precedents trials.)). I used the doAsync function of anko to reduce the quantity of code, but the AsyncTask.execute{}
works as well.
I hope some one will be able to use my answer in spite of my bad english.
Answered By - niaou suzanne
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.