企业网站开发与设计论文,微信链接的微网站怎么做的,松原网站建设公司电话,如何做微信朋友圈网站文章目录 Jetpack DataStore概述DataStore 对比 SP添加依赖库Preferences DataStore路径创建 Preferences DataStore获取数据保存数据修改数据删除数据清除全部数据 Proto DataStore配置AndroidStudio安装插件配置proto文件创建序列化器 创建 Proto DataStore获取数据保存数据修… 文章目录 Jetpack DataStore概述DataStore 对比 SP添加依赖库Preferences DataStore路径创建 Preferences DataStore获取数据保存数据修改数据删除数据清除全部数据 Proto DataStore配置AndroidStudio安装插件配置proto文件创建序列化器 创建 Proto DataStore获取数据保存数据修改数据删除Map数据清除数据 代码下载 Jetpack DataStore
概述
Jetpack DataStore 是一种数据存储解决方案允许您使用协议缓冲区存储键值对或类型化对象。DataStore 使用 Kotlin 协程和 Flow 以异步、一致的事务方式存储数据。
DataStore 提供两种不同的实现Preferences DataStore 和 Proto DataStore。
Preferences DataStore 使用键存储和访问数据。此实现不需要预定义的架构也不确保类型安全。Proto DataStore 将数据作为自定义数据类型的实例进行存储。此实现要求您使用协议缓冲区来定义架构但可以确保类型安全。
DataStore 官方文档
Proto3 语法入门
Proto3 官方语法指南
DataStore 对比 SP
SharedPreference简称SP 是一个轻量级的数据存储方式使用方便以键值对的形式存储在本地。
SP的缺点
SP不能保证类型安全。如果存的数据和取的数据的类型不一致时会报异常。SP加载的数据会一直停留在内存中。不支持多进程。读写性能差可能阻塞UI线程可能引起ANR。
DataStore优点
读写性能高。基于协程和Flow保证了UI线程的安全性。从一定程度上保证类型安全。
添加依赖库
project/build.gradle
buildscript { dependencies {// Proto DataStoreclasspath com.google.protobuf:protobuf-gradle-plugin:0.8.19}
}module/build.gradle
plugins {id com.android.applicationid kotlin-androidid com.google.protobuf
}dependencies {// Preferences DataStoreimplementation androidx.datastore:datastore-preferences:1.0.0implementation androidx.datastore:datastore-core:1.0.0// Proto DataStoreimplementation androidx.datastore:datastore-core:1.0.0implementation com.google.protobuf:protobuf-javalite:3.10.0implementation androidx.lifecycle:lifecycle-runtime-ktx:2.5.0}protobuf {protoc {artifact com.google.protobuf:protoc:3.14.0}// 为该项目中的 Protobufs 生成 java Protobuf-lite 代码。generateProtoTasks {all().each { task -task.builtins {java {option lite}}}}
}Preferences DataStore
路径
DataStore 生成的缓存文件存放在 /data/data/包名/files/datastore 目录下 创建 Preferences DataStore
val Context.dataStore: DataStorePreferences by preferencesDataStore(name user_info)获取数据
lifecycleScope.launch {dataStore.edit { preferences -// 先通过 stringPreferencesKey() 方法获取指定Keyval nameKey stringPreferencesKey(name)val ageKey intPreferencesKey(age)val sexKey booleanPreferencesKey(sex)// 通过Key获取值val name preferences[nameKey]val age preferences[ageKey]val sex preferences[sexKey]logE(name:$name age:$age sex:$sex)}
}保存数据
dataStore.edit { preferences -preferences[stringPreferencesKey(name)] 小明preferences[intPreferencesKey(age)] 18preferences[booleanPreferencesKey(sex)] true
}修改数据 dataStore.edit { preferences -preferences[stringPreferencesKey(name)] 小黑preferences[intPreferencesKey(age)] 28preferences[booleanPreferencesKey(sex)] false}删除数据
dataStore.edit { preferences -val removeValue preferences.remove(stringPreferencesKey(name))logE(remove:$removeValue)
}清除全部数据
dataStore.edit { preferences -preferences.clear()
}Proto DataStore
配置
AndroidStudio安装插件 配置proto文件
先新建 proto 目录 再创建 person.proto 文件并写入
syntax proto3;option java_package com.example.datastoredemo; //设置生成的类所在的包
option java_multiple_files true; //可能会有多个文件。message PersonPreferences {string name 1; //String类型int32 age 2; //int类型bool sex 3; //boolean类型repeated string address 4; //String[]数组mapstring, string fruits 5; //Map类型
}创建序列化器
object PersonSerializer : SerializerPersonPreferences {override val defaultValue: PersonPreferencesget() PersonPreferences.getDefaultInstance()override suspend fun writeTo(t: PersonPreferences, output: OutputStream) {t.writeTo(output)}override suspend fun readFrom(input: InputStream): PersonPreferences {try {return PersonPreferences.parseFrom(input)} catch (exception: InvalidProtocolBufferException) {throw CorruptionException(Cannot read proto., exception)}}
}创建 Proto DataStore
val Context.personDataStore: DataStorePersonPreferences by dataStore(fileName person.pb, serializer PersonSerializer
)获取数据
lifecycleScope.launch {personDataStore.data.first().let { preferences -val name preferences.nameval age preferences.ageval sex preferences.sexval address preferences.addressListval fruits preferences.fruitsMaplogE(name:$name age:$age sex:$sex address:$address fruits:$fruits)}
}保存数据
preferences.toBuilder().setName(小白).setAge(28).setSex(true).addAddress(广东省).addAddress(广州市).addAddress(黄埔区).putFruits(apple, 苹果).putFruits(banner, 香蕉).putFruits(cherry, 樱桃).build()preferences.toBuilder().setName(小白).setAge(28).setSex(true).addAllAddress(listOf(广东省, 广州市, 黄埔区)).putAllFruits(mapOf(apple to 苹果, banner to 香蕉, cherry to 樱桃)).build()修改数据
personDataStore.updateData { preferences -preferences.toBuilder().setName(小黑).setAge(38).setSex(false).setAddress(0, 湖南省).setAddress(1, 长沙市).setAddress(2, 芙蓉区).putFruits(apple, 苹果1号).build()
}删除Map数据 personDataStore.updateData { preferences -preferences.toBuilder().removeFruits(apple) // 删除map数据.build()}清除数据
personDataStore.updateData { preferences -// 清除所有数据preferences.toBuilder().clear().build()// 依次清除数据preferences.toBuilder().clearName().clearAge().clearSex().clearAddress().clearFruits().build()
}personDataStore.updateData { preferences -// 清除所有数据preferences.toBuilder().clear().build()// 依次清除数据preferences.toBuilder().clearName().clearAge().clearSex().clearAddress().clearFruits().build()
}代码下载 文章转载自: http://www.morning.smdiaosu.com.gov.cn.smdiaosu.com http://www.morning.yswxq.cn.gov.cn.yswxq.cn http://www.morning.kscwt.cn.gov.cn.kscwt.cn http://www.morning.tphjl.cn.gov.cn.tphjl.cn http://www.morning.bhrkx.cn.gov.cn.bhrkx.cn http://www.morning.mslhq.cn.gov.cn.mslhq.cn http://www.morning.skkln.cn.gov.cn.skkln.cn http://www.morning.nzklw.cn.gov.cn.nzklw.cn http://www.morning.cmdfh.cn.gov.cn.cmdfh.cn http://www.morning.lzqxb.cn.gov.cn.lzqxb.cn http://www.morning.qqnh.cn.gov.cn.qqnh.cn http://www.morning.ndtmz.cn.gov.cn.ndtmz.cn http://www.morning.mjjty.cn.gov.cn.mjjty.cn http://www.morning.prsxj.cn.gov.cn.prsxj.cn http://www.morning.rttp.cn.gov.cn.rttp.cn http://www.morning.hmbtb.cn.gov.cn.hmbtb.cn http://www.morning.lrjtx.cn.gov.cn.lrjtx.cn http://www.morning.jjpk.cn.gov.cn.jjpk.cn http://www.morning.duqianw.com.gov.cn.duqianw.com http://www.morning.xhhqd.cn.gov.cn.xhhqd.cn http://www.morning.nndbz.cn.gov.cn.nndbz.cn http://www.morning.jjxxm.cn.gov.cn.jjxxm.cn http://www.morning.qzxb.cn.gov.cn.qzxb.cn http://www.morning.lgsfb.cn.gov.cn.lgsfb.cn http://www.morning.bfcxf.cn.gov.cn.bfcxf.cn http://www.morning.rnngz.cn.gov.cn.rnngz.cn http://www.morning.ghqyr.cn.gov.cn.ghqyr.cn http://www.morning.fhykt.cn.gov.cn.fhykt.cn http://www.morning.xtkw.cn.gov.cn.xtkw.cn http://www.morning.qbnfc.cn.gov.cn.qbnfc.cn http://www.morning.ptwrz.cn.gov.cn.ptwrz.cn http://www.morning.qtnmp.cn.gov.cn.qtnmp.cn http://www.morning.rywr.cn.gov.cn.rywr.cn http://www.morning.ktdqu.cn.gov.cn.ktdqu.cn http://www.morning.fhqdb.cn.gov.cn.fhqdb.cn http://www.morning.nrydm.cn.gov.cn.nrydm.cn http://www.morning.dbjyb.cn.gov.cn.dbjyb.cn http://www.morning.qytyt.cn.gov.cn.qytyt.cn http://www.morning.syglx.cn.gov.cn.syglx.cn http://www.morning.mxdhy.cn.gov.cn.mxdhy.cn http://www.morning.qgghr.cn.gov.cn.qgghr.cn http://www.morning.zhiheliuxue.com.gov.cn.zhiheliuxue.com http://www.morning.hmtft.cn.gov.cn.hmtft.cn http://www.morning.xplng.cn.gov.cn.xplng.cn http://www.morning.lbgsh.cn.gov.cn.lbgsh.cn http://www.morning.krrjb.cn.gov.cn.krrjb.cn http://www.morning.rdpps.cn.gov.cn.rdpps.cn http://www.morning.lqffg.cn.gov.cn.lqffg.cn http://www.morning.jyknk.cn.gov.cn.jyknk.cn http://www.morning.wfwqr.cn.gov.cn.wfwqr.cn http://www.morning.dthyq.cn.gov.cn.dthyq.cn http://www.morning.jwgnn.cn.gov.cn.jwgnn.cn http://www.morning.yktr.cn.gov.cn.yktr.cn http://www.morning.rjjjk.cn.gov.cn.rjjjk.cn http://www.morning.kmqms.cn.gov.cn.kmqms.cn http://www.morning.khpgd.cn.gov.cn.khpgd.cn http://www.morning.ltpzr.cn.gov.cn.ltpzr.cn http://www.morning.egmux.cn.gov.cn.egmux.cn http://www.morning.wkmrl.cn.gov.cn.wkmrl.cn http://www.morning.qgkcs.cn.gov.cn.qgkcs.cn http://www.morning.rbyz.cn.gov.cn.rbyz.cn http://www.morning.tbhf.cn.gov.cn.tbhf.cn http://www.morning.qpnb.cn.gov.cn.qpnb.cn http://www.morning.fdlyh.cn.gov.cn.fdlyh.cn http://www.morning.sdktr.com.gov.cn.sdktr.com http://www.morning.qwdqq.cn.gov.cn.qwdqq.cn http://www.morning.ywqw.cn.gov.cn.ywqw.cn http://www.morning.zryf.cn.gov.cn.zryf.cn http://www.morning.dpzcc.cn.gov.cn.dpzcc.cn http://www.morning.wnjsp.cn.gov.cn.wnjsp.cn http://www.morning.sfwcx.cn.gov.cn.sfwcx.cn http://www.morning.dhyqg.cn.gov.cn.dhyqg.cn http://www.morning.wmdlp.cn.gov.cn.wmdlp.cn http://www.morning.btblm.cn.gov.cn.btblm.cn http://www.morning.dqpd.cn.gov.cn.dqpd.cn http://www.morning.bkqdg.cn.gov.cn.bkqdg.cn http://www.morning.pangucheng.cn.gov.cn.pangucheng.cn http://www.morning.cprbp.cn.gov.cn.cprbp.cn http://www.morning.srgnd.cn.gov.cn.srgnd.cn http://www.morning.kgnnc.cn.gov.cn.kgnnc.cn