chat-sdk / chat-sdk-android

Chat SDK Android - Open Source Mobile Messenger

Home Page:https://chatsdk.co

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to clear local database "andorid-chatsdk-database" using ChatSDK?

anonym24 opened this issue · comments

ChatSDK has a bug when re-signing on the same device with different users, it's related to database, if you reinstall the app fully before signing to a new user, then everything is ok, but if you do not, and re-signing to the same app instance with another user then we get the next error when these users try to open a thread with each other:

val threadID = createThreadIdFromIdsOfUsers(ChatSDK.currentUser().entityID, otherUser.entityID)
ChatSDK.thread().createThread(null, listOf(user), ThreadType.Private1to1, threadID)
private fun createThreadIdFromIdsOfUsers(ourUserId: String, otherUserId: String): String {
    return sortString("$ourUserId$otherUserId")
}

private fun sortString(str: String): String {
    return str.toCharArray().sorted().joinToString(separator = "")
}

I receive ThreadID without any issues, but when it's being used in ChatActivity, it can't get Thread object by this id, some issue with cache, reinstalling the app helps

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.Date sdk.chat.core.dao.Thread.getLoadMessagesFrom()' on a null object reference
        at sdk.chat.ui.views.ChatView.onLoadMore(ChatView.java:296)
        at sdk.chat.ui.views.ChatView.initViews(ChatView.java:220)
        at sdk.chat.ui.activities.ChatActivity.initViews(ChatActivity.java:240)

so between ChatkSDK logout (old user) and ChatSDK login (new user) I have to clear/reset database of ChatSDK (or only threads and messages in cache) to avoid this bug. But I can't find any method to clear cache. I tried to look at ChatSDK.db() methods but there is nothing

ChatSDK version - 5.1.14

my temporary solution on app logout do next:

        if (ChatSDK.shared().isActive) {
            ChatSDK.shared().stop()
        }
        context.deleteDatabase("andorid-chatsdk-database")
        val intent = context.packageManager.getLaunchIntentForPackage(context.packageName)!!
        context.startActivity(Intent.makeRestartActivityTask(intent.component))
        exitProcess(0)