当前位置:网站首页>Map structure stored in the room of jetpack series
Map structure stored in the room of jetpack series
2022-07-23 14:53:00 【When it rains at night in Bashan】
cause
Recently, I made a demand , Need to put the equipment ( You can't connect to the Internet ) Data of middle buried point , It is transmitted to App, then App Then upload to the buried point platform , My plan is , Store every buried point in the database , then App When selecting a machine to read , The device reads the database and returns json strand ,App After receiving the data , Conduct SDK Buried point .
work
Build table , Event name , Parameters , Time stamp , So the entity classes are as follows :
@Entity(tableName = "app_point_log")
data class AppPointLogEntity(
var eventName: String,
var eventParams: Map<String, String>,
var timeStamp: Long
) {
@PrimaryKey(autoGenerate = true)
@ColumnInfo(name = "id")
var id: Long = 0
}
Then write DAO as well as Repository layer , I won't repeat it here . After writing , Manually click compile , An error is as follows :
error : Cannot figure out how to save this field into database. You can consider adding a type converter for it. - eventParams in com.xxxx.libdomain.data.point.AppPointLogEntity
error : Cannot figure out how to read this field from a cursor. - eventParams in com.xxxx.libdomain.data.point.AppPointLogEntity
Is the meaning clear enough , It is suggested to add a converter
This Converter as follows :
class MapTypeConverter {
@TypeConverter
fun stringToMap(value: String): Map<String, String> {
return Gson().fromJson(value, object : TypeToken<Map<String, String>>() {
}.type)
}
@TypeConverter
fun mapToString(value: Map<String, String>?): String {
return if (value == null) "" else Gson().toJson(value)
}
}
After you've written , You must remember , Add it to this place
@Database(entities = [AppPointLogEntity::class], version = 1, exportSchema = false)
@TypeConverters(MapTypeConverter::class)
abstract class AppDatabase : RoomDatabase() {
abstract fun appPointLogDao(): AppPointLogDao
}
Compile again , No longer an error , Get it done !!!
边栏推荐
- win11安装系统提示virtualBox不兼容需要卸载virtual的解决办法,但是卸载列表找不到virtual的解决办法
- 【无标题】
- 自研的数据产品迭代了一年多,为什么不买第三方商业数据平台产品呢?
- First acquaintance and search set
- Oracle 报表常用sql
- 【小程序自动化Minium】一、框架介绍和环境搭建
- [test platform development] XVII. The interface editing page realizes the drop-down cascade selection, and binds the module to which the interface belongs
- Authing 支持 Zadig 啦!云原生用户统一认证快速对接
- OpenCV计算外包矩形
- 直播课堂系统03-model类及实体
猜你喜欢
随机推荐
Detailed tutorial of typora drawing bed configuration
[applet automation minium] i. framework introduction and environment construction
Cmake notes
Building personal network disk based on nextcloud
一道代码题看 CommonJS 模块化规范
Transferred from Yuxi information disclosure: products such as mRNA covid-19 vaccine and Jiuzhou horse tetanus immunoglobulin are expected to be on the market within this year.
俄方希望有效落实农产品外运“一揽子”协议
【测试平台开发】十七、接口编辑页面实现下拉级联选择,绑定接口所属模块...
Jetpack系列之Room中存Map结构
linux定时备份数据库脚本
[test platform development] XVII. The interface editing page realizes the drop-down cascade selection, and binds the module to which the interface belongs
[software testing] how to sort out your testing business
【无标题】测试【无标题】测试
【C语言】猜数字小游戏+关机小程序
[record of question brushing] 19. Delete the penultimate node of the linked list
【我可以做你的第一个项目吗?】GZIP的细节简介和模拟实现
Ffmpeg 2 - use of ffplay, ffprobe, ffmpeg commands
MySQL unique index has no duplicate value, and the error is repeated
Quick introduction to PKI system
win11安装系统提示virtualBox不兼容需要卸载virtual的解决办法,但是卸载列表找不到virtual的解决办法








