Flow를 이용해서 데이터 변화를 감지해보겠습니다.

id 'kotlin-kapt'
// ROOM
def roomVersion = "2.5.0"
implementation("androidx.room:room-runtime:$roomVersion")
annotationProcessor("androidx.room:room-compiler:$roomVersion")
// To use Kotlin annotation processing tool (kapt)
kapt("androidx.room:room-compiler:$roomVersion")
// Coroutine
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.4")
// Coroutine + Room
implementation("androidx.room:room-ktx:$roomVersion")
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MainActivity : AppCompatActivity() { | |
private lateinit var viewModel: MainViewModel | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
viewModel = ViewModelProvider(this).get(MainViewModel::class.java) | |
val inputArea = findViewById<EditText>(R.id.textInputArea) | |
val insertBtn = findViewById<Button>(R.id.insert) | |
val getAllBtn = findViewById<Button>(R.id.getData) | |
val deleteBtn = findViewById<Button>(R.id.delete) | |
val resultArea = findViewById<TextView>(R.id.resultArea) | |
val db = TextDatabase.getDatabase(this) | |
insertBtn.setOnClickListener { | |
CoroutineScope(Dispatchers.IO).launch { | |
db.textDao().insert(TextEntity(0, inputArea.text.toString())) | |
inputArea.setText("") | |
} | |
} | |
getAllBtn.setOnClickListener { | |
CoroutineScope(Dispatchers.IO).launch { | |
// val resultText = db.textDao().getAllDataNorMal().toString() | |
// | |
// withContext(Dispatchers.Main) { | |
// resultArea.text = resultText | |
// } | |
viewModel.readData().collect { | |
val resultText = it.toString() | |
withContext(Dispatchers.Main) { | |
resultArea.text = resultText | |
} | |
} | |
} | |
} | |
deleteBtn.setOnClickListener { | |
CoroutineScope(Dispatchers.IO).launch { | |
db.textDao().deleteAllData() | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MainViewModel(application: Application) : AndroidViewModel(application) { | |
val context = getApplication<Application>().applicationContext | |
val db = TextDatabase.getDatabase(context) | |
fun readData() : Flow<List<TextEntity>> = db.textDao().getAllDataFlow() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Dao | |
interface TextDao { | |
@Query("SELECT * FROM text_table") | |
fun getAllDataNorMal() : List<TextEntity> | |
@Query("SELECT * FROM text_table") | |
fun getAllDataFlow() : Flow<List<TextEntity>> | |
@Insert(onConflict = OnConflictStrategy.IGNORE) | |
fun insert(text : TextEntity) | |
@Query("DELETE FROM text_table") | |
fun deleteAllData() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Database(entities = [TextEntity::class], version = 1) | |
abstract class TextDatabase : RoomDatabase() { | |
abstract fun textDao() : TextDao | |
companion object { | |
@Volatile | |
private var INSTANCE : TextDatabase? = null | |
fun getDatabase( | |
context : Context | |
) : TextDatabase { | |
return INSTANCE ?: synchronized(this){ | |
val instance = Room.databaseBuilder( | |
context.applicationContext, | |
TextDatabase::class.java, | |
"text_database" | |
) | |
.fallbackToDestructiveMigration() | |
.build() | |
INSTANCE = instance | |
instance | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Entity(tableName = "text_table") | |
data class TextEntity ( | |
@PrimaryKey(autoGenerate = true) | |
@ColumnInfo(name = "id") | |
var id : Int, | |
@ColumnInfo(name = "text") | |
var text : String | |
) |
'Android Jetpack' 카테고리의 다른 글
ROOM + Coroutine Flow - 7 (Room + Flow + ListAdapter CRUD) (0) | 2023.01.29 |
---|---|
ROOM + Coroutine Flow - 6 (Room + Flow + ListAdapter) (0) | 2023.01.28 |
ROOM + Coroutine Flow - 3 (Why Room & Coroutine - 1) (0) | 2023.01.27 |
ROOM + Coroutine Flow - 2 (ListAdapter / DiffUtil Ex) (0) | 2023.01.25 |
ROOM + Coroutine Flow - 1 (ListAdapter / DiffUtil 이란?) (0) | 2023.01.24 |