Junior
What database do you use in your projects?
sobes.tech AI
Answer from AI
Used several options depending on project requirements:
- Room Persistent Library: Most often. Provides a convenient abstract layer over SQLite, significantly reducing boilerplate code and ensuring query validation at compile time. Ideal for most typical Android application scenarios.
- SQLite directly: In older projects or when very low-level access and full control over schemas and queries were required. Requires more manual work.
- Realm: In several projects where performance was important for handling large volumes of data and complex objects. Realm is an object-oriented database, which is convenient when working with data models.
- Firebase Realtime Database / Cloud Firestore: For projects that require real-time data synchronization between devices and cloud storage. The choice between them depended on data structure and query needs. Realtime Database is well-suited for JSON tree data, while Cloud Firestore is suitable for document collections.
The choice of a specific database is determined by factors such as:
- Volume and type of data.
- Query complexity.
- Performance requirements.
- Data synchronization needs.
- Team expertise level.
- Availability of ready-made solutions and libraries.
For example, a typical implementation with Room includes:
// entity
@Entity(tableName = "users")
data class User(
@PrimaryKey val id: Int,
val name: String
)
// dao
@Dao
interface UserDao {
@Query("SELECT * FROM users")
fun getAll(): Flow<List<User>>
@Insert(onConflict = OnConflictStrategy.IGNORE)
suspend fun insert(user: User)
}
// database
@Database(entities = [User::class], version = 1)
abstract class AppDatabase : RoomDatabase() {
abstract fun userDao(): UserDao
}