【发布时间】:2021-11-09 01:26:23
【问题描述】:
我有这个while循环,问题是如果条件为假,它将永远不会中断并继续打印为真我正在使用firebase来跟踪值
private var gLTF: Boolean? = null
myRef.addValueEventListener(object : ValueEventListener {
override fun onDataChange(dataSnapshot: DataSnapshot) {
var gsLvlR = dataSnapshot.child("gLvl").value.toString()
var gLt = gsLvlR.toInt()
CoroutineScope(Dispatchers.Default).launch {
while (checkGsValue(gLt)) {
println("true")
if (!checkGsValue(gLt)) {
println("false")
break
}
delay(15000)
}
}
}
override fun onCancelled(error: DatabaseError) {
// Failed to read value
}
})
//Function
fun checkGsValue(gL: Int): Boolean {
gLTF = gL > 250
return gLTF as Boolean
}
这里是 logcat 的结果
2021-11-09 04:20:25.888 27709-27757/com.example.testing I/System.out: true
2021-11-09 04:20:40.890 27709-27757/com.example.testing I/System.out: true
2021-11-09 04:20:55.893 27709-27757/com.example.testing I/System.out: true
2021-11-09 04:21:10.894 27709-27757/com.example.testing I/System.out: true
2021-11-09 04:21:25.897 27709-27757/com.example.testing I/System.out: true
2021-11-09 04:21:40.899 27709-27757/com.example.testing I/System.out: true
2021-11-09 04:21:55.901 27709-27757/com.example.testing I/System.out: true
2021-11-09 04:22:10.902 27709-27757/com.example.testing I/System.out: true
2021-11-09 04:22:25.904 27709-27757/com.example.testing I/System.out: true
【问题讨论】:
-
我建议你也打印
gLt值