In my activity i have an Observer that observes this query
@Query("""Select *,
(Select account_id from accounts where account_id = from_account_id) as contact_account_id,
(Select first_name from accounts where account_id = from_account_id) as contact_first_name,
(Select last_name from accounts where account_id = from_account_id) as contact_last_name
from messages inner join messageQueue on messages.client_message_id = messagequeue.client_message_id
where ((msg_type = 1 and body <> "") or msg_type <> 1)
order by message_date desc
""")
fun getChatRoomGroupMessages(): DataSource.Factory<Int, ChatMessageItem>
ChatItem is a relation of 2 tables
@Embedded
var message: MessageDto? = null
@Relation(parentColumn = "client_message_id", entityColumn = "client_message_id", entity = MessageQueueDto::class)
var messageQueue: MessageQueueDto? = null
'client_message_id' is the primary key of table messages and its autoincrement.
When i want to 'send' a message i create an entry in message i get the client_message_id that the insert returns, set it to messageQueue and create an entry there so i can have the relation.
viewModelScope.launch(Dispatchers.IO) {
val clientMessageIdList = insertMessages(messageDtoList)
for (i in clientMessageIdList.indices) {
messageQueueDtoList[i].clientMessageId = clientMessageIdList[i]
}
insertMessageQueues(messageQueueDtoList)
}
When the objects get inserted to DB, the LiveData objerver triggers and i see the message in my screen
Everything works fine until i want to send an image. In order to do that i have a process that creates messages and messagequeues inside a workmanager. But when i do the same thing (save to messsages and then to messagequeue) then my Observer does not get triggered.
class FileStorage @AssistedInject constructor(
@Assisted private val appContext: Context,
@Assisted private val params: WorkerParameters,
private val messagesRepository: MessagesRepository
) : CoroutineWorker(appContext, params) {
................
val headerClientMessageId =
messagesRepository.insertMessages(headerFileMessagesArray)
for (i in headerClientMessageId.indices)
headerFileMessageQueuesArray[i].clientMessageId =
headerClientMessageId[i]
messagesRepository.insertMessageQueues(headerFileMessageQueuesArray)
}
As soon as i make a new insert, then i see the previous inserts along with the new. I use Dagger2 for Injection and i have declared my Database and my Daos and my repositories as Singletons.
Does anybody has a clue for what is happening here??