Skip to content

Commit 931a499

Browse files
committed
Fix occasional crash after adding a contact
Sometimes, the coroutine adding the contact to the database wouldn't have finished before we navigated to the chat for the new contact, leading to a crash. Now we wait until that coroutine is done before navigating. This crash could easily be reproduced by adding a delay(1000) to `ContactManager::add`.
1 parent 3074a36 commit 931a499

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

atox/src/main/kotlin/ui/addcontact/AddContactViewModel.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import java.util.Date
1111
import javax.inject.Inject
1212
import kotlinx.coroutines.CoroutineScope
1313
import kotlinx.coroutines.launch
14+
import kotlinx.coroutines.runBlocking
1415
import ltd.evilcorp.atox.tox.ToxStarter
1516
import ltd.evilcorp.core.repository.MessageRepository
1617
import ltd.evilcorp.core.vo.Contact
@@ -48,8 +49,8 @@ class AddContactViewModel @Inject constructor(
4849
)
4950
}
5051

51-
fun addContact(toxId: ToxID, message: String) {
52-
contactManager.add(toxId, message)
52+
fun addContact(toxId: ToxID, message: String) = runBlocking {
53+
contactManager.add(toxId, message).join()
5354
addToChatLog(toxId.toPublicKey().string(), message)
5455
}
5556
}

0 commit comments

Comments
 (0)