0

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??

james04
  • 1,580
  • 2
  • 20
  • 46

1 Answers1

0

Finally my problem was with Dagger injection. In my application class i was setting tw different instances of AppDaggerComponent and inject those to the application and the WorkManager.

// Before
        DaggerAppComponent.factory().withContext(this).inject(this)

        val factory: AppWorkerFactory = DaggerAppComponent.factory().withContext(this).workerFactory()
        WorkManager.initialize(this, Configuration.Builder().setWorkerFactory(factory).build())

// After
        val daggerAppComponent = DaggerAppComponent.factory().withContext(this)
        daggerAppComponent.inject(this)

        val factory: AppWorkerFactory = daggerAppComponent.workerFactory()
        WorkManager.initialize(this, Configuration.Builder().setWorkerFactory(factory).build())

PROBLEM SOLVED...!!

james04
  • 1,580
  • 2
  • 20
  • 46