79093527

Date: 2024-10-16 10:06:09
Score: 1.5
Natty:
Report link

The answer is the answer to this post.

Basically all my tests with flows would have worked. The problem was that I wasn't applying a singleton from the repository (I also tried using a localdatasource at startup but I wanted to remove a level to make testing easier, with the same problem, of course).

So when I debugged adding new tasks it was true that the StateFlow stored the new tasks correctly, but the flow I got in getTask was a different flow from a different instance of TaskRepository.

I was using two data sources of which one was getting and the other one was updating being totally independent.

Once I added the @Singleton tag in the hilt provider of the repository everything worked correctly.

@InstallIn(SingletonComponent::class)
@Module
class DataModule() {

    @Singleton // This was the fix
    @Provides
    fun provideTaskRepository(localDataSource: TaskLocalDataSource): TaskRepository =
        TaskRepositoryImpl(localDataSource)
}

PD: ty @MichalP

Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • User mentioned (1): @Singleton
  • User mentioned (0): @MichalP
  • Self-answer (0.5):
  • Low reputation (1):
Posted by: Diego oo