Is it possible for some singletons to outlive other singletons

20 views Asked by At

In android applications, is it possible for some singletons to outlive other singletons?

To add more context, I am using hilt to provide my singletons. An example taken from my actual application.

...
    @Provides
    @Singleton
    fun provideDatabaseValidator(
        @ApplicationContext context: Context
    ): XRoomDatabaseValidator {
        return XRoomDatabaseValidator(context)
    }

    @Provides
    @Singleton
    fun provideDatabase(
        @ApplicationContext context: Context, 
        validator: XRoomDatabaseValidator
    ): XRoomDatabase {
        return XRoomDatabase.getDatabase(context, validator)
    }
...

XRoomDatabaseValidator validates the XRoomDatabase by having fields that is an anonymous implementation of RoomDatabase.Callback and RoomDatabase.PrepackagedDatabaseCallback; this fields are then used as callbacks in creating the database using Room.databaseBuilder

XRoomDatabaseValidator keeps some information about the db, like a flag that is set if the db was just created, or created from file, etc.

What I was worrying is if XRoomDatabase outlives XRoomDatabaseValidator, then XRoomDatabaseValidator now has the wrong information of XRoomDatabase; more specifically the initial values of the flags.

0

There are 0 answers