找人做网站都需要提供什么,搜索引擎有哪些软件,网站建设哪个便宜,网站建设功能报价表问题描述
当我编写quickupload库时#xff0c;因为需要在 Service中进行上传任务#xff0c;向Service传递时我发现需要传递的数据很多并且结构复杂#xff0c;如果处理不好就会导致以下几个问题
耗时: 需要更多时间进行开发和测试以确保正确的数据处理。容易出错: 由于手…问题描述
当我编写quickupload库时因为需要在 Service中进行上传任务向Service传递时我发现需要传递的数据很多并且结构复杂如果处理不好就会导致以下几个问题
耗时: 需要更多时间进行开发和测试以确保正确的数据处理。容易出错: 由于手动序列化和反序列化逻辑出现错误的风险更高。维护: 维护和更新代码的工作量增加尤其是数据结构发生变化时。
解决方案
为了解决这个问题需要编写 PersistableData
open class PersistableData() : Parcelable {protected val data HashMapString, Any()override fun equals(other: Any?): Boolean {if (other null || other !is PersistableData) return falsereturn data other.data}override fun hashCode() data.hashCode()SuppressLint(ParcelClassLoader)private constructor(parcel: Parcel) : this() {parcel.readBundle()?.let { bundle -bundle.keySet().forEach { key -when (val value bundle[key]) {is Boolean, is Double, is Int, is Long, is String - data[key] value}}}}override fun describeContents() 0override fun writeToParcel(dest: Parcel, flags: Int) {toBundle().writeToParcel(dest, flags)}companion object CREATOR : Parcelable.CreatorPersistableData {private const val separator $override fun createFromParcel(parcel: Parcel) PersistableData(parcel)override fun newArray(size: Int): ArrayPersistableData? arrayOfNulls(size)/*** 从PersistableData JSON表示创建 [PersistableData]。*/JvmStaticfun fromJson(rawJsonString: String): PersistableData {val json JSONObject(rawJsonString)val data PersistableData()json.keys().forEach { key -when (val value json.get(key)) {is Boolean, is Double, is Int, is Long, is String - data.data[key] value}}return data}}private fun String.validated(checkExists: Boolean false): String {if (contains(separator))throw IllegalArgumentException(key cannot contain $separator as its a reserved character, used for nested data)if (checkExists !data.containsKey(this))throw IllegalArgumentException(no data found for key \$this\)return this}fun putBoolean(key: String, value: Boolean) {data[key.validated()] value}fun getBoolean(key: String) data[key.validated(checkExists true)] as Booleanfun putDouble(key: String, value: Double) {data[key.validated()] value}fun getDouble(key: String) data[key.validated(checkExists true)] as Doublefun putInt(key: String, value: Int) {data[key.validated()] value}fun getInt(key: String) data[key.validated(checkExists true)] as Intfun putLong(key: String, value: Long) {data[key.validated()] value}fun getLong(key: String) data[key.validated(checkExists true)] as Longfun putString(key: String, value: String) {data[key.validated()] value}fun getString(key: String) data[key.validated(checkExists true)] as Stringfun putData(key: String, data: PersistableData) {data.data.forEach { (dataKey, value) -this.data[$key$separator$dataKey] value}}fun getData(key: String): PersistableData {val entries data.entries.filter { it.key.startsWith($key$separator) }if (entries.isEmpty()) return PersistableData()return PersistableData().also { extractedData -entries.forEach { (entryKey, entryValue) -extractedData.data[entryKey.removePrefix($key$separator)] entryValue}}}fun putArrayData(key: String, data: ListPersistableData) {data.forEachIndexed { index, persistableData -persistableData.data.forEach { (dataKey, value) -this.data[$key$separator$index$separator$dataKey] value}}}fun getArrayData(key: String): ListPersistableData {val entries ArrayList(data.entries.filter { it.key.startsWith($key$separator) })if (entries.isEmpty()) return emptyList()var index 0var elements entries.filter { it.key.startsWith($key$separator$index$separator) }val outList ArrayListPersistableData()while (elements.isNotEmpty()) {outList.add(PersistableData().also { extractedData -elements.forEach { (entryKey, entryValue) -extractedData.data[entryKey.removePrefix($key$separator$index$separator)] entryValue}entries.removeAll(elements)})index 1elements entries.filter { it.key.startsWith($key$separator$index$separator) }}return outList}/*** 创建一个新的包其中包含此 [PersistableData] 中存在的所有字段。*/fun toBundle() Bundle().also { bundle -data.keys.forEach { key -when (val value data[key]) {is Boolean - bundle.putBoolean(key, value)is Double - bundle.putDouble(key, value)is Int - bundle.putInt(key, value)is Long - bundle.putLong(key, value)is String - bundle.putString(key, value)}}}/*** 创建一个包含所有字段的JSON字符串表示* 在此 [PersistableData] 中。** 这并不意味着人类可读而是一种方便的方式来传递复杂的* 使用字符串的结构化数据。*/fun toJson() JSONObject().also { json -data.keys.forEach { key -when (val value data[key]) {is Boolean, is Double, is Int, is Long, is String - json.put(key, value)}}}.toString()
}简单总结一下
存储各种类型的键值对Boolean、Double、Int、Long、String。提供放入和获取数据的方法putBoolean、getBoolean等。使用带有分隔符的键支持嵌套和数组数据结构。 转换数据为Bundle和JSON格式便于存储和检索。实现Parcelable接口允许在Android组件之间传递数据。
为了在使用时保持统一性
编写接口 Persistable
interface Persistable {fun toPersistableData(): PersistableDatainterface CreatorT {fun createFromPersistableData(data: PersistableData): T}
}简单总结一下
定义了一个可以转换为PersistableData对象的契约。确保数据对象的序列化和反序列化方式一致。
当需要对某个数据类进行序列化时只需要实现接口 Persistable例如 UploadFile数据类
Parcelize
data class UploadFile JvmOverloads constructor(val path: String,val properties: LinkedHashMapString, String LinkedHashMap()
) : Parcelable, Persistable {companion object : Persistable.CreatorUploadFile {private object CodingKeys {const val path pathconst val properties props}override fun createFromPersistableData(data: PersistableData) UploadFile(path data.getString(CodingKeys.path),properties LinkedHashMapString, String().apply {val bundle data.getData(CodingKeys.properties).toBundle()bundle.keySet().forEach { propKey -put(propKey, bundle.getString(propKey)!!)}})}override fun toPersistableData() PersistableData().apply {putString(CodingKeys.path, path)putData(CodingKeys.properties, PersistableData().apply {properties.entries.forEach { (propKey, propVal) -putString(propKey, propVal)}})}
}简单总结一下
使用PersistableData存储其属性使得状态的保存和恢复变得简单。包含一个自定义Creator用于从PersistableData创建UploadFile实例。
总结
我认为这样做 可以简化和优化在Android应用中管理复杂数据结构及其持久化的过程如果对你有帮助记得点赞收藏 文章转载自: http://www.morning.wqpb.cn.gov.cn.wqpb.cn http://www.morning.mjqms.cn.gov.cn.mjqms.cn http://www.morning.fndmk.cn.gov.cn.fndmk.cn http://www.morning.cpgdy.cn.gov.cn.cpgdy.cn http://www.morning.jfch.cn.gov.cn.jfch.cn http://www.morning.rgzc.cn.gov.cn.rgzc.cn http://www.morning.pqwrg.cn.gov.cn.pqwrg.cn http://www.morning.zwndt.cn.gov.cn.zwndt.cn http://www.morning.qzmnr.cn.gov.cn.qzmnr.cn http://www.morning.sqfnx.cn.gov.cn.sqfnx.cn http://www.morning.brfxt.cn.gov.cn.brfxt.cn http://www.morning.lgpzq.cn.gov.cn.lgpzq.cn http://www.morning.smxyw.cn.gov.cn.smxyw.cn http://www.morning.knjj.cn.gov.cn.knjj.cn http://www.morning.kqxwm.cn.gov.cn.kqxwm.cn http://www.morning.mwkwg.cn.gov.cn.mwkwg.cn http://www.morning.wjtwn.cn.gov.cn.wjtwn.cn http://www.morning.lhgqc.cn.gov.cn.lhgqc.cn http://www.morning.c7617.cn.gov.cn.c7617.cn http://www.morning.xnnpy.cn.gov.cn.xnnpy.cn http://www.morning.sqxr.cn.gov.cn.sqxr.cn http://www.morning.byshd.cn.gov.cn.byshd.cn http://www.morning.bzjpn.cn.gov.cn.bzjpn.cn http://www.morning.dblfl.cn.gov.cn.dblfl.cn http://www.morning.wphfl.cn.gov.cn.wphfl.cn http://www.morning.tlzbt.cn.gov.cn.tlzbt.cn http://www.morning.bpmtr.cn.gov.cn.bpmtr.cn http://www.morning.hxbps.cn.gov.cn.hxbps.cn http://www.morning.kyzja.com.gov.cn.kyzja.com http://www.morning.lfmwt.cn.gov.cn.lfmwt.cn http://www.morning.bbrf.cn.gov.cn.bbrf.cn http://www.morning.gcspr.cn.gov.cn.gcspr.cn http://www.morning.krhkn.cn.gov.cn.krhkn.cn http://www.morning.tphrx.cn.gov.cn.tphrx.cn http://www.morning.cnwpb.cn.gov.cn.cnwpb.cn http://www.morning.ntdzjx.com.gov.cn.ntdzjx.com http://www.morning.krrjb.cn.gov.cn.krrjb.cn http://www.morning.jhrqn.cn.gov.cn.jhrqn.cn http://www.morning.wypyl.cn.gov.cn.wypyl.cn http://www.morning.mmqng.cn.gov.cn.mmqng.cn http://www.morning.rddlz.cn.gov.cn.rddlz.cn http://www.morning.kpbgvaf.cn.gov.cn.kpbgvaf.cn http://www.morning.youngbase.cn.gov.cn.youngbase.cn http://www.morning.rdlong.com.gov.cn.rdlong.com http://www.morning.xbbrh.cn.gov.cn.xbbrh.cn http://www.morning.kdldx.cn.gov.cn.kdldx.cn http://www.morning.tpdg.cn.gov.cn.tpdg.cn http://www.morning.ygflz.cn.gov.cn.ygflz.cn http://www.morning.slysg.cn.gov.cn.slysg.cn http://www.morning.bpmnq.cn.gov.cn.bpmnq.cn http://www.morning.pjwml.cn.gov.cn.pjwml.cn http://www.morning.hhfwj.cn.gov.cn.hhfwj.cn http://www.morning.ggfdq.cn.gov.cn.ggfdq.cn http://www.morning.pcshb.cn.gov.cn.pcshb.cn http://www.morning.kdpal.cn.gov.cn.kdpal.cn http://www.morning.dxsyp.cn.gov.cn.dxsyp.cn http://www.morning.lmjkn.cn.gov.cn.lmjkn.cn http://www.morning.sjwws.cn.gov.cn.sjwws.cn http://www.morning.jxcwn.cn.gov.cn.jxcwn.cn http://www.morning.rzpkt.cn.gov.cn.rzpkt.cn http://www.morning.kkzwn.cn.gov.cn.kkzwn.cn http://www.morning.qdxwf.cn.gov.cn.qdxwf.cn http://www.morning.qwfl.cn.gov.cn.qwfl.cn http://www.morning.pnljy.cn.gov.cn.pnljy.cn http://www.morning.gcdzp.cn.gov.cn.gcdzp.cn http://www.morning.rxwnc.cn.gov.cn.rxwnc.cn http://www.morning.rxtxf.cn.gov.cn.rxtxf.cn http://www.morning.dphmj.cn.gov.cn.dphmj.cn http://www.morning.zpqk.cn.gov.cn.zpqk.cn http://www.morning.zcsyz.cn.gov.cn.zcsyz.cn http://www.morning.sfcfy.cn.gov.cn.sfcfy.cn http://www.morning.kuaijili.cn.gov.cn.kuaijili.cn http://www.morning.jfch.cn.gov.cn.jfch.cn http://www.morning.htfnz.cn.gov.cn.htfnz.cn http://www.morning.zbkdm.cn.gov.cn.zbkdm.cn http://www.morning.qgdsd.cn.gov.cn.qgdsd.cn http://www.morning.cwqpl.cn.gov.cn.cwqpl.cn http://www.morning.ampingdu.com.gov.cn.ampingdu.com http://www.morning.nccqs.cn.gov.cn.nccqs.cn http://www.morning.zlces.com.gov.cn.zlces.com