解决数据库异常情况

This commit is contained in:
2026-01-22 14:47:56 +08:00
parent 41d48a23af
commit f216e14338
@@ -13,6 +13,7 @@ import com.sw.inbound.utils.ImageUtil
import com.sw.inbound.utils.ext.toJsonString
import io.objectbox.Box
import io.objectbox.kotlin.boxFor
import io.objectbox.query.IdWithScore
import io.objectbox.query.Query
import org.pytorch.IValue
import org.pytorch.Module
@@ -117,13 +118,18 @@ object FoodModule {
box.query(Food_.foodVector.nearestNeighbors(floatArray, queryCount)).build()
//查询比较分数
// val tempList = query.findWithScores().sortedBy { it.score }.map { "${it.get().name}|${it.get().foodIdx}|${it.score}" }
val idScoreList = query.findIdsWithScores()
var idScoreList: List<IdWithScore>
try {
idScoreList = query.findIdsWithScores();
} finally {
// 先关闭Query,释放Cursor
query.close()
}
val nameScoreList = mutableListOf<IdNameScore>()
idScoreList.forEach {
nameScoreList.add(IdNameScore(id = it.id, name = box.get(it.id).name?:"", score = it.score))
}
Timber.tag("FoodModule").d("queryFood数据:${nameScoreList.toJsonString()}")
query.close()
return nameScoreList
}
fun getFoodScoreList(bitmap: Bitmap, queryCount: Int = 15): List<IdNameScore> {