优化统计
This commit is contained in:
@@ -125,7 +125,7 @@ class CollectSearchDialog(
|
|||||||
setOnItemClickListener { _, _, position ->
|
setOnItemClickListener { _, _, position ->
|
||||||
activity?.lifecycleScope?.launch {
|
activity?.lifecycleScope?.launch {
|
||||||
val goodsName = list[position].goodsName?:""
|
val goodsName = list[position].goodsName?:""
|
||||||
val count = ObjectBox.count(goodsName)
|
val count = ObjectBox.countNameField(goodsName)
|
||||||
activity?.toast("已采集数据${count}条")
|
activity?.toast("已采集数据${count}条")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -193,8 +193,7 @@ object FoodModule {
|
|||||||
|
|
||||||
suspend fun initDefFoodData(context: Context, action: () -> Unit = {}) {
|
suspend fun initDefFoodData(context: Context, action: () -> Unit = {}) {
|
||||||
try {
|
try {
|
||||||
val count =
|
val count = ObjectBox.countIndexField(DEFAULT_FOOD_INDEX)
|
||||||
ObjectBox.getBox<Food>()?.all?.count { it.foodIdx == DEFAULT_FOOD_INDEX } ?: 0
|
|
||||||
if (count > 0) {
|
if (count > 0) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -182,10 +182,14 @@ object ObjectBox {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 使用 QueryBuilder 在数据库层计数,避免全量加载到内存
|
// 使用 QueryBuilder 在数据库层计数,避免全量加载到内存
|
||||||
suspend fun count(name: String) = safeDbOp {
|
suspend fun countNameField(name: String) = safeDbOp {
|
||||||
getBox<Food>()?.query(Food_.name.equal(name))?.build()?.count()
|
getBox<Food>()?.query(Food_.name.equal(name))?.build()?.count()
|
||||||
} ?: 0L
|
} ?: 0L
|
||||||
|
|
||||||
|
suspend fun countIndexField(idx: Int) = safeDbOp {
|
||||||
|
getBox<Food>()?.query(Food_.foodIdx.equal(idx))?.build()?.count()
|
||||||
|
} ?: 0L
|
||||||
|
|
||||||
// 使用 QueryBuilder 在数据库层过滤,避免全量加载到内存
|
// 使用 QueryBuilder 在数据库层过滤,避免全量加载到内存
|
||||||
suspend fun filter(name: String?) = safeDbOp {
|
suspend fun filter(name: String?) = safeDbOp {
|
||||||
if (!name.isNullOrBlank()) {
|
if (!name.isNullOrBlank()) {
|
||||||
|
|||||||
Reference in New Issue
Block a user