'Multiple database update lags the UI android kotlin room coroutines
In my android chat application, i have a Table Entity called 'Messages'. In that table the body is a Json format object which holds some data. For example the json has the form of a FileHeader, which holds some information about a file inside the chat.
data class FileHeader(
@Expose
@SerializedName("k")
val fileName: String,
@Expose
@SerializedName("e")
val fileDate: String,
@Expose
@SerializedName("z")
val fileSize: Int,
@Expose
@SerializedName("h")
val fileHash: String,
@Expose
@SerializedName("t")
val totalSegments: Int,
@Expose
@SerializedName("s")
var lastDownloadedSegment: Int
}
As you can see, there is a field called lastDownloadedSegment
, which means, up to now how many segments have been uploaded/downloaded. So each time i download/upload a new file part i have a method that informs the fileHeader.
suspend fun updateFileHeaderMessage(serverId: Long, messageId: String, completeSegments: Int, status: Int) {
getFileHeader(serverId, messageId)?.let {
val newHeader = it.copy(lastDownloadedSegment = completeSegments, status = status, description = "")
messageRepository.updateBodyAndFileStatus(
serverId, messageId,
EncryptString(Gson().toJson(newHeader)), status
)
}
}
This function is called from a coroutine every time a new filepart get uploaded/downloaded (which is an information from a socket calledback).
class SocketResponseHandler {
private val scope = CoroutineScope(Dispatchers.Main + SupervisorJob())
fun webSocketResolver(message: String) {
scope.launch(Dispatchers.IO) {
// the suspend function that finally calls the updateFileHeaderMessage
fileHandler.filePartReceived(message)
}
}
}
In my UI i have a recyclerview. If the message is a File, then i have a progressBar that calculate its lastDownloadedSegment/totalSegments progress.
Generally, the files have about 2000 parts. So the update method gets called many many times. But why do i have a lag in the UI since i do this in a background coroutine thread?
Solution 1:[1]
Well, this question is invalid because after some research that i did seems that the problem is the DiffUtils onChangePayload that i use to update the UI. Coroutines and Db updates do not have any problem!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | james04 |