Tomasz Turkiewicz
Tomasz Turkiewicz

Reputation: 1

Android Kotlin Room different entities for different users

I'm facing small problem. I have an app (game) and I would like to create database using Room. Everything is fine as long as I have only one app user. What I would like to do is to have different databases or one database with different Entities for different logged in users. I'm using Firebase for authentication so I'm able to have userId (String) for each currently logged in User. My Entity class:

    @Entity
data class PointOnField(
                        @PrimaryKey(autoGenerate = false)
                        var position: Int = 0,
                        var moveUp: Int = Static.MOVE_AVAILABLE,
                        var moveUpRight: Int = Static.MOVE_AVAILABLE,
                        var moveRight: Int = Static.MOVE_AVAILABLE,
                        var moveDownRight: Int = Static.MOVE_AVAILABLE,
                        var moveDown: Int = Static.MOVE_AVAILABLE,
                        var moveDownLeft: Int = Static.MOVE_AVAILABLE,
                        var moveLeft: Int = Static.MOVE_AVAILABLE,
                        var moveUpLeft: Int = Static.MOVE_AVAILABLE,
                        var x:Int=0,
                        var y:Int=0,
                        var ball:Boolean = false)

My Dao class:

  @Dao
interface PointOnFieldDao {

    @Insert
    suspend fun addPointOnField(pointOnField: PointOnField)

    @Query("SELECT * FROM pointOnField")
    suspend fun getAllPointsOnField() : List<PointOnField>

    @Update
    suspend fun updatePointOnField(pointOnField: PointOnField)

}

My Database:

    @Database(
        entities = [PointOnField::class],
        version = 1
)
abstract class PointOnFieldEasyDatabase : RoomDatabase() {

    abstract fun getPointOnFiledDao() : PointOnFieldDao

    companion object{

        @Volatile private var instance : PointOnFieldEasyDatabase ?= null
        private val LOCK = Any()

        operator fun invoke(context: Context) = instance ?: synchronized(LOCK){
            instance ?: buildDatabase(context).also {
                instance = it
            }
        }

        private fun buildDatabase(context: Context) = Room.databaseBuilder(
                context.applicationContext,
                PointOnFieldEasyDatabase::class.java,
                "easydatabase"
        ).build()
    }
}

Upvotes: 0

Views: 311

Answers (1)

Hennadii Hlushkov
Hennadii Hlushkov

Reputation: 23

You can try to inherit all your @Entity from a class that contains user id information. And write a code that will add selection by current logged in UserId as you expected.

@Entity
data class PointOnField(
@PrimaryKey(autoGenerate = false)
var position: Int = 0,
var moveUp: Int = Static.MOVE_AVAILABLE,
var moveUpRight: Int = Static.MOVE_AVAILABLE,
var moveRight: Int = Static.MOVE_AVAILABLE,
var moveDownRight: Int = Static.MOVE_AVAILABLE,
var moveDown: Int = Static.MOVE_AVAILABLE,
var moveDownLeft: Int = Static.MOVE_AVAILABLE,
var moveLeft: Int = Static.MOVE_AVAILABLE,
var moveUpLeft: Int = Static.MOVE_AVAILABLE,
var x:Int=0,
var y:Int=0,
var ball:Boolean = false) : AuthorizedEntity()

class AuthorizedEntity(var userId: Long)

Upvotes: 0

Related Questions