contrast
SharedPreference
SP, for short, stores raw type data in key-value pairs, which are stored in XML files by default
- Application scenario: The storage capacity is small and simple
- Advantages and disadvantages: It has its own memory-level cache, which is fast to read when the data volume is small, but it is not safe for cross-process. When the data volume is large, it is slow to load, and full write is easy to cause ANR
Sqlite, Room
Room is not a database. It provides an abstraction layer on top of SQLite, allowing users to take advantage of sqLite’s power while enjoying a more robust database access mechanism. The flexible interface adaptation layer is retained.
@Database(entities = [KeyValue::class], version = 1, exportSchema = true)
abstract class KvStore : RoomDatabase(), CoroutineScope by MainScope() {
companion object {
val database by lazy {
Room.databaseBuilder(
SwUtils.application, KvStore::class.java, "key_value_database.db"
)
.allowMainThreadQueries()
.build()
}
fun <T : Any> get(key: String, defaultValue: T): T {
val type = defaultValue::class.simpleName ? :""
val value = database.dao().get(key, type)
return try {
val v = when (defaultValue) {
is String -> value
is Boolean -> value == "true"
is Int -> value.toInt()
is Float -> value.toFloat()
is Double -> value.toDouble()
else -> Gson().fromJson(value, defaultValue::class.java)
}
v as T
} catch (e: Throwable) {
defaultValue
}
}
fun <T : Any> set(key: String, value: T) {
val type = value::class.simpleName ? :""
val v = when (value) {
is String, is Boolean.is Int.is Float.is Double -> value.toString()
else -> Gson().toJson(value)
}
database.dao().insertReplace(KeyValue(key, type, v))
}
inline fun <reified T> liveData(key: String, sticky: Boolean = true): MediatorLiveData<T> {
var realSticky = sticky
val type = T::class.simpleName ? :""
val mld = MediatorLiveData<T>()
mld.addSource(database.dao().liveData(key, type)) {
if (realSticky) {
mld.value = try {
Gson().fromJson(it, T::class.java)
} catch (e: Throwable) {
null}}else {
realSticky = true}}return mld
}
}
abstract fun dao(a): KeyValueDao
}
@Entity(primaryKeys = ["key"."type"])
data class KeyValue(
@ColumnInfo(name = "key", defaultValue = "")
var key: String = "".@ColumnInfo(name = "type", defaultValue = "")
var type: String = "".@ColumnInfo(name = "value", defaultValue = "")
var value: String = ""
)
@Dao
abstract class KeyValueDao : BaseDao<KeyValue>() {
@Query("SELECT value from KeyValue WHERE `key` = :key AND type = :type")
abstract fun get(key: String, type: String): String
@Query("SELECT value from KeyValue WHERE `key` = :key AND type = :type")
abstract fun liveData(key: String, type: String): LiveData<String>
@Insert(onConflict = OnConflictStrategy.REPLACE)
abstract fun insertReplace(obj: KeyValue): Long
}
Copy the code
The last
You can copy code or extend the benefits according to this idea:
- There is no need to introduce additional tripartite libraries
- On the basis of KV storage, data monitoring is added by using room characteristics