Commit 66ddf21e by zhangchengbo

fix:优化foreach,list集合效率

parent 56263fa2
......@@ -38,6 +38,8 @@ import kotlinx.coroutines.launch
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import java.util.Collections
import java.util.WeakHashMap
class MainActivity : SimpleActivity() {
......@@ -49,7 +51,8 @@ class MainActivity : SimpleActivity() {
private var lastSearchedText = ""
private var bus: EventBus? = null
private var wasProtectionHandled = false
private val listMessageId = arrayListOf<Long>()
// private val listMessageId = arrayListOf<Long>()
private val listMessageId = Collections.newSetFromMap(WeakHashMap<Long, Boolean>())
private val binding by viewBinding(ActivityMainBinding::inflate)
......@@ -361,14 +364,15 @@ class MainActivity : SimpleActivity() {
Log.d(TAG, " getNewConversations 获取系统SMS DB短信列表(脏数据) conversations:${GsonUtil.parseListToJson(conversations)}")
//通过DB数据与cache对比threadID
val threadIdSet = cachedConversations.map { it.threadId }.toSet()
conversations.forEach { clonedConversation ->
val threadIds = cachedConversations.map { it.threadId }
if (!threadIds.contains(clonedConversation.threadId)) {
if (!threadIdSet.contains(clonedConversation.threadId)) { // O(1) 查找
conversationsDB.insertOrUpdate(clonedConversation)
cachedConversations.add(clonedConversation)
}
}
cachedConversations.forEach { cachedConversation ->
val threadId = cachedConversation.threadId
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment