Trying to fix concurrent exception in contacts loader

This commit is contained in:
Sylvain Berfini 2022-04-11 11:38:42 +02:00
parent 2ab24f893a
commit 4c83e823fc

View file

@ -56,8 +56,6 @@ class ContactLoader : LoaderManager.LoaderCallbacks<Cursor> {
) )
} }
private val friends = HashMap<String, Friend>()
override fun onCreateLoader(id: Int, args: Bundle?): Loader<Cursor> { override fun onCreateLoader(id: Int, args: Bundle?): Loader<Cursor> {
Log.i("[Contacts Loader] Loader created") Log.i("[Contacts Loader] Loader created")
coreContext.contactsManager.fetchInProgress.value = true coreContext.contactsManager.fetchInProgress.value = true
@ -74,11 +72,12 @@ class ContactLoader : LoaderManager.LoaderCallbacks<Cursor> {
override fun onLoadFinished(loader: Loader<Cursor>, cursor: Cursor) { override fun onLoadFinished(loader: Loader<Cursor>, cursor: Cursor) {
Log.i("[Contacts Loader] Load finished, found ${cursor.count} entries in cursor") Log.i("[Contacts Loader] Load finished, found ${cursor.count} entries in cursor")
friends.clear()
val core = coreContext.core val core = coreContext.core
val linphoneMime = loader.context.getString(R.string.linphone_address_mime_type) val linphoneMime = loader.context.getString(R.string.linphone_address_mime_type)
coreContext.lifecycleScope.launch { coreContext.lifecycleScope.launch {
val friends = HashMap<String, Friend>()
withContext(Dispatchers.IO) { withContext(Dispatchers.IO) {
while (!cursor.isClosed && cursor.moveToNext()) { while (!cursor.isClosed && cursor.moveToNext()) {
try { try {
@ -191,13 +190,15 @@ class ContactLoader : LoaderManager.LoaderCallbacks<Cursor> {
} }
if (fl != core.defaultFriendList) core.addFriendList(fl) if (fl != core.defaultFriendList) core.addFriendList(fl)
for (friend in friends.values) {
val friendsList = friends.values
for (friend in friendsList) {
fl.addLocalFriend(friend) fl.addLocalFriend(friend)
} }
fl.updateSubscriptions() fl.updateSubscriptions()
Log.i("[Contacts Loader] Friends added & subscription updated") Log.i("[Contacts Loader] Friends added & subscription updated")
friends.clear()
coreContext.contactsManager.fetchFinished() coreContext.contactsManager.fetchFinished()
} }
} }