当前位置:网站首页>Jetpack系列之Room中存Map结构
Jetpack系列之Room中存Map结构
2022-07-23 09:25:00 【去话巴山夜雨时】
起因
最近做一个需求,需要把设备(不能联外网)中埋点数据,通过局域网传给App,然后App再上传到埋点平台,我给出的方案是,把产生的每一条埋点都存入数据库,然后App择机读取的时候,设备这边读取数据库返回json串,App端拿到数据后,进行SDK埋点。
干活
建表,事件名,参数,时间戳,所以实体类如下:
@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
}
接着写DAO以及Repository层,这里就不赘述了。写好之后,手动点击编译,报错如下:
错误: 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
错误: Cannot figure out how to read this field from a cursor. - eventParams in com.xxxx.libdomain.data.point.AppPointLogEntity
意思够明白吧,建议添加一个converter
这个Converter如下:
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)
}
}
写完之后,必须记得,把它加在这个地方
@Database(entities = [AppPointLogEntity::class], version = 1, exportSchema = false)
@TypeConverters(MapTypeConverter::class)
abstract class AppDatabase : RoomDatabase() {
abstract fun appPointLogDao(): AppPointLogDao
}
再次编译,不再报错,搞定!!!
边栏推荐
- Is it risky and safe to open a mobile stock account?
- Chapter 3 complex query
- Authing supports Zadig! Unified authentication and rapid docking of cloud native users
- 中望CAD专业版 2022软件安装包下载及安装教程
- 【测试平台开发】十七、接口编辑页面实现下拉级联选择,绑定接口所属模块...
- Wacom firmware update error 123, digital board driver cannot be updated
- Which is a good fixed asset management system? What are the fixed asset management platforms?
- FFmpeg 1 - 概览/安装
- What is per title encoding?
- First acquaintance and search set
猜你喜欢

Authing 支持 Zadig 啦!云原生用户统一认证快速对接

AI acceleration gesture recognition experience based on efr32mg24

Sword finger offer19 regular expression

JS calendar style pie chart statistics plug-in

对象使用过程中背后调用了哪些方法

Sword finger offer 46. translate numbers into strings
![[applet automation minium] i. framework introduction and environment construction](/img/1f/95b78e6574c3af3ff7abcf5db838f5.png)
[applet automation minium] i. framework introduction and environment construction

Uni app knowledge points and records of problems and solutions encountered in the project

After using vscode to format the code, save and find that the code is messy again. What should I do? Vs remove formatting

Palindrome related topics
随机推荐
Towhee weekly model
R language practical application case: drawing part (III) - drawing of multiple combination patterns
Optimize Huawei ECs to use key login
OKRK3399開發板預留I2C4掛載EEPROM
Design and implementation of websocket universal packaging
LZ77文件压缩
[applet automation minium] i. framework introduction and environment construction
【C語言】猜數字小遊戲+關機小程序
Dynamic programming -- knapsack problem
Official wechat product! Applet automation framework minium sharing Preview
对象使用过程中背后调用了哪些方法
【WinForm】关于截图识别数字并计算的桌面程序实现方案
关于flex布局justify-content:space-around最后一个不对齐的解决方法和为什么这样子解决是讨论
Quanzhi f1c100s/f1c200s learning notes (13) -- lvgl transplantation
Zhongwang CAD professional 2022 software installation package download and installation tutorial
Using JS to parse and execute XSS automatically
CSDN写文方法(二)
webstrom ERROR in [eslint] ESLint is not a constructor
CSDN writing method (II)
基于nextcloud构建个人网盘