。
通常の状況では、アプリケーションにいくつかのデフォルトの構成情報があり、一部のスイッチボタンやその他の軽量データのステータスなどのユーザーの設定情報もSharedPreferencesファイルに保存されます。この記事では、実装を変更しようとしています。 。デフォルトのデータはローカルデータベースに保存され、アプリケーションのさまざまなバージョンの開発プロセスで、これらのデータの追加、削除、変更、およびクエリ操作を可能な限り便利かつ簡単に処理できることが望まれます。
ユーザーのデフォルトの構成情報をjsonファイルに書き込みます。これらのデータは、アプリケーションの起動とユーザーのログイン後にデータベースに書き込まれます。将来のさまざまなバージョンのアプリケーションの開発では、関連するjsonを操作するだけで済みます。データベース内のデータを実現するためのファイル。いくつかの構成情報を追加、変更、削除し、
サンプルコードは、例としてObjectBoxデータベースを取り上げています。この記事は、実装のアイデアのみを提供し、参照のみを目的としています。
。
jsonファイルの例は次のとおりです。
構成情報のuser_config.jsonファイル:
[
{
"pkey": "user_setting",
"ckey": "",
"value": 0,
"version": 1,
"operationType": 1
},
{
"pkey": "app_setting",
"ckey": "",
"value": 0,
"version": 1,
"operationType": 1
},
{
"pkey": "notify",
"ckey": "app_setting",
"value": 1,
"version": 1,
"operationType": 1
},
{
"pkey": "voice",
"ckey": "user_setting",
"value": 1,
"version": 1,
"operationType": 1
},
{
"pkey": "vibrate",
"ckey": "user_setting",
"value": 1,
"version": 1,
"operationType": 1
}
]
复制代码
この例のデータ構造jsonはマルチレベル分類関係を採用しており、データベースもこの方法で保存されます。これは実際のニーズに基づいています。「pkey」は一意であり、各構成には一意の名前「」が必要です。 ckey "これは、それが属する上位分類に関連付けられています。" value "は、合意された値であり、スイッチボタンのオン/オフステータスなどの構成のステータス値を表します。" version "は、バージョン番号を表します。合意された値である「operationType」は、オブジェクトの現在の操作タイプ(追加、削除など)を表します。
テーブルに対応するバージョン番号のtable_version.jsonファイル:
{
"user_config_entity": 1
}
复制代码
テーブルに対応するバージョン番号はマップ形式で保存されます。「user_config_entity」はユーザー構成情報テーブルに対応することに同意します。他の同様の機能要件がある場合は、他のテーブルに対応するバージョン情報も追加できます。このセットに。
ユーザーが初めて構成情報を初期化するとき、jsonファイルが最初に解析され、デフォルトのデータがローカルデータベースに保存されます。
当后续应用已经开发到了其它版本,假如应用在开发版本6的时候需要添加一个新的按钮的默认数据,只需要在 user_config.json 文件中加入一个json 对象即可,如:
{
"pkey": "message",
"ckey": "user_setting",
"value": 1,
"version": 2,
"operationType": 1
}
复制代码
注意,这里必须要修改 "version" 这个字段值,要比之前的版本大,同时要修改 table_version.json 中 "user_config_entity" 表的版本号,一定要保证这两个值一致,这个版本号是我们自己定义的,与实际使用的数据库版本号没有任何关系,是用来标识我们的配置信息数据是否有新的数据需要处理;另外约定的 operationType = 1代表数据库中会新增这条数据。
假如应用在开发版本6的时候需要删除一个旧的按钮的数据,同样需要像上面一样修改版本号,operationType = 2代表数据库中会删除这条数据。
另外我们也可以将版本号的值都降低,也是可以的,根据自己对不同配置数据的需求进行处理即可。
通过操作json文件,就实现了在数据库中增加、修改、删除这些配置信息,这其中一个重要的依赖字段是 "operationType" ,它可以帮助我们方便的在业务处理时判断是新增、修改还是删除等操作;
而后续应用的版本还会不断的迭代,不同的版本这些配置信息很可能会发生化,有的新增,有的删除,为了能尽可能的只处理当前开发版本所涉及的数据,同时用户所安装的应用可能有跨版本升级的情况,这就是 "version" 字段 和 "table_version" 的意义。
下面主要看一下在页面处理逻辑:
package com.windfallsheng.handleuserconfiginfos.view;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import com.windfallsheng.handleuserconfiginfos.Constants;
import com.windfallsheng.handleuserconfiginfos.R;
import com.windfallsheng.handleuserconfiginfos.db.manager.UserConfigManager;
import com.windfallsheng.handleuserconfiginfos.db.UserConfigService;
import com.windfallsheng.handleuserconfiginfos.db.entity.UserConfigEntity;
import com.windfallsheng.handleuserconfiginfos.widget.SwitchButton;
public class MainActivity extends AppCompatActivity {
private static final String TAG = MainActivity.class.getSimpleName();
private SwitchButton mSwitchBtnNotify, mSwitchBtnVoice, mSwitchBtnVibrate;
private TextView mTvNotify, mTvVoice, mTvVibrate;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//
mSwitchBtnNotify = findViewById(R.id.switchbutton_notify);
mSwitchBtnVoice = findViewById(R.id.switchbutton_voice);
mSwitchBtnVibrate = findViewById(R.id.switchbutton_vibrate);
mTvNotify = findViewById(R.id.textview_notify_status);
mTvVoice = findViewById(R.id.textview_voice_status);
mTvVibrate = findViewById(R.id.textview_vibrate_status);
// 初始化相关配置信息
UserConfigService userConfigService = new UserConfigService(this);
userConfigService.initUserConfig();
// 通知设置
final UserConfigEntity notifyConfig = UserConfigManager.getInstance().queryUserConfigByKey(Constants.CONFIG_KEY_NOTIFY);
Log.d(TAG, "method:onCreate#notifyConfig=" + notifyConfig);
if (notifyConfig == null) {
mTvNotify.setText(getResources().getString(R.string.config_delete));
} else {
int notifyValue = notifyConfig.getValue();
Log.d(TAG, "method:onCreate#notifyValue=" + notifyValue);
final boolean notifyChecked = notifyValue == Constants.SWITCH_ON ? true : false;
mSwitchBtnNotify.setChecked(notifyChecked);
// 监听事件
mSwitchBtnNotify.setOnCheckedChangeListener(new SwitchButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(SwitchButton view, boolean isChecked) {
Log.d(TAG, "method:onCreate#notifyChecked=" + notifyChecked + ", isChecked=" + isChecked);
int switchStatus = isChecked == true ? Constants.SWITCH_ON : Constants.SWITCH_OFF;
notifyConfig.setValue(switchStatus);
UserConfigManager.getInstance().updateUserConfig(notifyConfig);
}
});
}
// 声音设置
final UserConfigEntity voiceConfig = UserConfigManager.getInstance().queryUserConfigByKey(Constants.CONFIG_KEY_VOICE);
Log.d(TAG, "method:onCreate#voiceConfig=" + voiceConfig);
if (voiceConfig == null) {
mTvVoice.setText(getResources().getString(R.string.config_delete));
} else {
final int voiceValue = voiceConfig.getValue();
Log.d(TAG, "method:onCreate#voiceValue=" + voiceValue);
final boolean voiceChecked = voiceValue == Constants.SWITCH_ON ? true : false;
mSwitchBtnVoice.setChecked(voiceChecked);
// 监听事件
mSwitchBtnVoice.setOnCheckedChangeListener(new SwitchButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(SwitchButton view, boolean isChecked) {
Log.d(TAG, "method:onCreate#voiceChecked=" + voiceChecked + ", isChecked=" + isChecked);
int switchStatus = isChecked == true ? Constants.SWITCH_ON : Constants.SWITCH_OFF;
voiceConfig.setValue(switchStatus);
UserConfigManager.getInstance().updateUserConfig(voiceConfig);
}
});
}
// 震动设置
final UserConfigEntity vibrateConfig = UserConfigManager.getInstance().queryUserConfigByKey(Constants.CONFIG_KEY_VIBRATE);
Log.d(TAG, "method:onCreate#vibrateConfig=" + vibrateConfig);
if (vibrateConfig == null) {
mTvVibrate.setText(getResources().getString(R.string.config_delete));
} else {
final int vibrateValue = vibrateConfig.getValue();
Log.d(TAG, "method:onCreate#vibrateValue=" + vibrateValue);
final boolean vibrateChecked = vibrateValue == Constants.SWITCH_ON ? true : false;
mSwitchBtnVibrate.setChecked(vibrateChecked);
// 监听事件
mSwitchBtnVibrate.setOnCheckedChangeListener(new SwitchButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(SwitchButton view, boolean isChecked) {
Log.d(TAG, "method:onCreate#vibrateChecked=" + vibrateChecked + ", isChecked=" + isChecked);
int switchStatus = isChecked == true ? Constants.SWITCH_ON : Constants.SWITCH_OFF;
vibrateConfig.setValue(switchStatus);
UserConfigManager.getInstance().updateUserConfig(vibrateConfig);
}
});
}
}
}
复制代码
数据库实体类如下:
package com.windfallsheng.handleuserconfiginfos.db.entity;
import java.io.Serializable;
import io.objectbox.annotation.Entity;
import io.objectbox.annotation.Id;
import io.objectbox.annotation.Index;
import io.objectbox.annotation.NameInDb;
/**
* @author lzsheng
* 用户个人的设置数据表
*/
@Entity
public class UserConfigEntity implements Serializable {
@Id
private long id;
/**
* 设置字段的key
*/
@Index
@NameInDb("pkey")
private String pkey;
/**
* 关联主键{@link UserConfigEntity#pkey}的字段
*/
@NameInDb("ckey")
private String ckey;
/**
* 设置字段的value
*/
@NameInDb("value")
private int value;
/**
* 最近修改时间
*/
@NameInDb("modify_time")
private long modifyTime;
/**
* 该数据做修改操作时对应的表版本号
*/
@NameInDb("version")
private int version;
/**
* 对该字段进行的操作的类型:修改、删除等
*/
@NameInDb("operation_type")
private int operationType;
public UserConfigEntity() {
}
public UserConfigEntity(long id, String pkey, String ckey, int value, long modifyTime, int version, int operationType) {
this.id = id;
this.pkey = pkey;
this.ckey = ckey;
this.value = value;
this.modifyTime = modifyTime;
this.version = version;
this.operationType = operationType;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getPkey() {
return pkey;
}
public void setPkey(String pkey) {
this.pkey = pkey;
}
public String getCkey() {
return ckey;
}
public void setCkey(String ckey) {
this.ckey = ckey;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public long getModifyTime() {
return modifyTime;
}
public void setModifyTime(long modifyTime) {
this.modifyTime = modifyTime;
}
public int getVersion() {
return version;
}
public void setVersion(int version) {
this.version = version;
}
public int getOperationType() {
return operationType;
}
public void setOperationType(int operationType) {
this.operationType = operationType;
}
@Override
public String toString() {
return "UserConfigEntity{" +
"id=" + id +
", pkey='" + pkey + '\'' +
", ckey='" + ckey + '\'' +
", value=" + value +
", modifyTime=" + modifyTime +
", version=" + version +
", operationType=" + operationType +
'}';
}
}
复制代码
package com.windfallsheng.handleuserconfiginfos.db.entity;
import java.io.Serializable;
import io.objectbox.annotation.Entity;
import io.objectbox.annotation.Id;
import io.objectbox.annotation.Index;
import io.objectbox.annotation.NameInDb;
import io.objectbox.annotation.Unique;
/**
* @author lzsheng
* 数据表及版本号
*/
@Entity
public class TableVersionEntity implements Serializable {
@Id
private long id;
@Index
@NameInDb("table_name")
/**
* 表名,每个表对应唯一的表名
*/
@Unique
private String tableName;
/**
* 该表对应的当前版本
*/
@NameInDb("version_code")
private int versionCode;
public TableVersionEntity() {
}
public TableVersionEntity(long id, String tableName, int versionCode) {
this.id = id;
this.tableName = tableName;
this.versionCode = versionCode;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getTableName() {
return tableName;
}
public void setTableName(String tableName) {
this.tableName = tableName;
}
public int getVersionCode() {
return versionCode;
}
public void setVersionCode(int versionCode) {
this.versionCode = versionCode;
}
@Override
public String toString() {
return "TableVersionEntity{" +
"id=" + id +
", tableName='" + tableName + '\'' +
", versionCode=" + versionCode +
'}';
}
}
复制代码
以下是数据库业务操作相关类的代码
用户配置数据的数据库操作类:
package com.windfallsheng.handleuserconfiginfos.db.manager;
import android.text.TextUtils;
import android.util.Log;
import com.windfallsheng.handleuserconfiginfos.db.ObjectBox;
import com.windfallsheng.handleuserconfiginfos.db.entity.UserConfigEntity;
import com.windfallsheng.handleuserconfiginfos.db.entity.UserConfigEntity_;
import java.util.List;
import io.objectbox.Box;
/**
* @author lzsheng
*
* {@link UserConfigEntity}的CRUD操作
*/
public class UserConfigManager {
private static final String TAG = UserConfigManager.class.getSimpleName();
private static volatile UserConfigManager instance = null;
private static Box<UserConfigEntity> mUserConfigEntityBox;
private UserConfigManager() {
}
public static UserConfigManager getInstance() {
if (instance == null) {
synchronized (UserConfigManager.class) {
if (instance == null) {
instance = new UserConfigManager();
mUserConfigEntityBox = ObjectBox.getBoxStore().boxFor(UserConfigEntity.class);
}
}
}
return instance;
}
public void insertUserConfigList(List<UserConfigEntity> userConfigList) {
Log.d(TAG, "method:insertUserConfigList#userConfigList=" + userConfigList.toString());
if (userConfigList == null) {
throw new IllegalArgumentException("userConfigList = null");
}
mUserConfigEntityBox.put(userConfigList);
}
public long insertOrReplaceUserConfig(UserConfigEntity userConfig) {
Log.d(TAG, "method:insertOrReplaceUserConfig#userConfig=" + userConfig);
if (userConfig == null) {
throw new IllegalArgumentException("userConfig = null");
}
UserConfigEntity userConfigEntity = queryUserConfigByKey(userConfig.getPkey());
if (userConfigEntity != null){
long id = userConfigEntity.getId();
Log.d(TAG, "method:insertOrReplaceUserConfig#userConfigEntity#id=" + id);
userConfig.setId(id);
}
long putId = mUserConfigEntityBox.put(userConfig);
Log.d(TAG, "method:insertOrReplaceUserConfig#putId=" + putId);
return putId;
}
public long updateUserConfig(UserConfigEntity userConfig) {
Log.d(TAG, "method:updateUserConfig#userConfig=" + userConfig);
if (userConfig == null) {
throw new IllegalArgumentException("userConfig = null");
}
long putId = mUserConfigEntityBox.put(userConfig);
Log.d(TAG, "method:updateUserConfig#putId=" + putId);
return putId;
}
/**
* 查询数据
*
* @return
*/
public UserConfigEntity queryUserConfigByKey(String pkey) {
Log.d(TAG, "queryUserConfigByKey#pkey=" + pkey);
if (TextUtils.isEmpty(pkey)) {
return null;
}
UserConfigEntity userConfigEntity = mUserConfigEntityBox
.query()
.equal(UserConfigEntity_.pkey, pkey)
.build()
.findUnique();
return userConfigEntity;
}
/**
* 查询所有配置数据
*
* @return
*/
public List<UserConfigEntity> queryAllUserConfig() {
Log.d(TAG, "queryAllUserConfig");
List<UserConfigEntity> userConfigList = mUserConfigEntityBox
.query()
.build()
.find();
return userConfigList;
}
public long queryCountUserConfig() {
long count = mUserConfigEntityBox
.query()
.build()
.count();
return count;
}
public long deleteUserConfigByKey(String pkey) {
Log.d(TAG, "method:deleteUserConfigByKey#pkey=" + pkey);
if (TextUtils.isEmpty(pkey)) {
throw new IllegalArgumentException("pkey is empty.");
}
long removeRowId = mUserConfigEntityBox
.query()
.equal(UserConfigEntity_.pkey, pkey)
.build()
.remove();
Log.d(TAG, "method:deleteUserConfigByKey#removeRowId=" + removeRowId);
return removeRowId;
}
}
复制代码
表的版本信息的数据库操作类:
package com.windfallsheng.handleuserconfiginfos.db.manager;
import android.text.TextUtils;
import android.util.Log;
import com.windfallsheng.handleuserconfiginfos.db.ObjectBox;
import com.windfallsheng.handleuserconfiginfos.db.entity.TableVersionEntity;
import com.windfallsheng.handleuserconfiginfos.db.entity.TableVersionEntity_;
import java.util.List;
import io.objectbox.Box;
/**
* @author lzsheng
* <p>
* {@link TableVersionEntity}的CRUD操作
*/
public class TableVersionManager {
private static final String TAG = TableVersionManager.class.getSimpleName();
private static volatile TableVersionManager instance = null;
private static Box<TableVersionEntity> mUserConfigEntityBox;
private TableVersionManager() {
}
public static TableVersionManager getInstance() {
if (instance == null) {
synchronized (TableVersionManager.class) {
if (instance == null) {
instance = new TableVersionManager();
mUserConfigEntityBox = ObjectBox.getBoxStore().boxFor(TableVersionEntity.class);
}
}
}
return instance;
}
public void insertOrReplaceTableVersion(TableVersionEntity tableVersion) {
Log.d(TAG, "insertOrReplaceTableVersion#tableVersion=" + tableVersion);
if (tableVersion == null) {
throw new IllegalArgumentException("tableVersion = null");
}
long putId = mUserConfigEntityBox
.put(tableVersion);
}
/**
* 查询数据
*
* @return
*/
public TableVersionEntity queryTableInfosByTableName(String tableName) {
Log.d(TAG, "method:queryTableInfosByTableName#argument:tableName=" + tableName);
if (TextUtils.isEmpty(tableName)) {
throw new IllegalArgumentException("tableName is empty");
}
TableVersionEntity tableVersionEntity = mUserConfigEntityBox
.query()
.equal(TableVersionEntity_.tableName, tableName)
.build()
.findUnique();
return tableVersionEntity;
}
public List<TableVersionEntity> queryAllTableVersion() {
Log.d(TAG, "queryAllTableVersion");
List<TableVersionEntity> tableVersionList = mUserConfigEntityBox
.query()
.build()
.find();
return tableVersionList;
}
public void deleteTableVersionByTableName(String tableName) {
Log.d(TAG, "deleteTableVersionByTableName#tableName=" + tableName);
if (TextUtils.isEmpty(tableName)) {
throw new IllegalArgumentException("tableName is empty");
}
long removeId = mUserConfigEntityBox
.query()
.equal(TableVersionEntity_.tableName, tableName)
.build()
.remove();
}
}
复制代码
UserConfigService 类处理json文件数据和数据库数据的交互业务,根据最新的数据判断如何处理
package com.windfallsheng.handleuserconfiginfos.db;
import android.content.Context;
import android.util.Log;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.windfallsheng.handleuserconfiginfos.Constants;
import com.windfallsheng.handleuserconfiginfos.Util.ResUtils;
import com.windfallsheng.handleuserconfiginfos.db.entity.TableVersionEntity;
import com.windfallsheng.handleuserconfiginfos.db.entity.UserConfigEntity;
import com.windfallsheng.handleuserconfiginfos.db.manager.TableVersionManager;
import com.windfallsheng.handleuserconfiginfos.db.manager.UserConfigManager;
import java.util.List;
import java.util.Map;
/**
* @author lzsheng
* 设置表的相关业务操作包括:
* 初始化用户设置的原始数据;更新数据库中对应的表的版本号等
*/
public class UserConfigService {
private static final String TAG = UserConfigService.class.getSimpleName();
private Context mContext;
public UserConfigService(Context context) {
if (context == null) {
throw new IllegalArgumentException("context = null");
}
this.mContext = context.getApplicationContext();
}
/**
* 初始化用户设置的原始数据,业务操作与设置表的版本号相关
*/
public void initUserConfig() {
Log.d(TAG, "method:initUserConfig#in");
printData();
// 获取配置文件中最新的版本信息
Map<String, Integer> newTableVersionMap = parseNewTableVersionMap();
// 获取json配置文件中最新的版本号
int newTableVersionCode = newTableVersionMap.get(Constants.TABLE_NAME_USER_CONFIG);
// 获取数据库中上一个版本信息
TableVersionEntity tableVersionEntity = TableVersionManager.getInstance().queryTableInfosByTableName(Constants.TABLE_NAME_USER_CONFIG);
Log.d(TAG, "method:initUserConfig#tableVersionEntity=" + tableVersionEntity + ", newVersionCode=" + newTableVersionCode + ", newTableVersionMap=" + newTableVersionMap);
// 当数据库中没有该表的版本号,说明还没有插入过任何数据,则可以直接向数据库插入所有数据
if (tableVersionEntity == null) {
List<UserConfigEntity> newUserConfigList = parseNewUserConfigList();
// 剔除不需要新增或者修改的对象
int size = newUserConfigList.size();
for (int i = 0; i < size; i++) {
UserConfigEntity userConfigEntity = newUserConfigList.get(i);
if (userConfigEntity.getOperationType() != Constants.USER_CONFIG_FIELD_OPERATION_TYPE_INSERT_OR_UPDATE) {
newUserConfigList.remove(i);
i--;
size--;
}
}
UserConfigManager.getInstance().insertUserConfigList(newUserConfigList);
// 更新数据库中的版本信息为最新版本
// if (...) { // 这里可以做一些验证,保证数据库的操作都正确之后再更新这个表的字段
updateTableVersion(0, Constants.TABLE_NAME_USER_CONFIG, newTableVersionCode);
// }
printData();
return;
}
// 当数据库中有该表的版本号时,则需要判断版本号的大小,是否需要处理新数据
int lastTableVersionCode = tableVersionEntity.getVersionCode();
Log.d(TAG, "method:initUserConfig#lastTableVersionCode=" + lastTableVersionCode + ", newTableVersionCode=" + newTableVersionCode);
// 当前数据库中的数据版本和最新数据的版本号相同,不需要处理新数据
if (newTableVersionCode == lastTableVersionCode) {
printData();
Log.d(TAG, "method:initUserConfig#return#newTableVersionCode == lastTableVersionCode");
return;
}
List<UserConfigEntity> newUserConfigList = parseNewUserConfigList();
// 升级操作:当前数据库中的数据版本小于最新数据的版本,需要处理新数据
if (newTableVersionCode > lastTableVersionCode) {
for (int i = 0; i < newUserConfigList.size(); i++) {
UserConfigEntity newUserConfig = newUserConfigList.get(i);
// 该数据的版本号
int newFieldVersionCode = newUserConfig.getVersion();
// 该数据的版本号大于上一个版本时,根据操作类型修改,或者删除
if (newFieldVersionCode > lastTableVersionCode) {
//根据数据的操作类型调用相应的修改和删除操作等
handleUserConfigByOperationType(newUserConfig);
}
}
// 更新数据库中的版本信息为最新版本
// if (...) { // 这里可以做一些验证,保证数据库的操作都正确之后再更新这个表的字段
updateTableVersion(tableVersionEntity.getId(), Constants.TABLE_NAME_USER_CONFIG, newTableVersionCode);
// }
printData();
Log.d(TAG, "method:initUserConfig#return#newTableVersionCode > lastTableVersionCode");
return;
}
// 降级操作:当前数据库中的数据版本大于最新数据的版本,需要处理新数据
if (newTableVersionCode < lastTableVersionCode) {
for (int i = 0; i < newUserConfigList.size(); i++) {
UserConfigEntity newUserConfig = newUserConfigList.get(i);
// 该数据的版本号
int newFieldVersionCode = newUserConfig.getVersion();
// 该数据的版本号小于上一个版本时,根据操作类型修改,或者删除
if (newFieldVersionCode < lastTableVersionCode) {
//根据数据的操作类型调用相应的修改和删除操作等
handleUserConfigByOperationType(newUserConfig);
}
}
// 更新数据库中的版本信息为最新版本
// if (...) { // 这里可以做一些验证,保证数据库的操作都正确之后再更新这个表的字段
updateTableVersion(tableVersionEntity.getId(), Constants.TABLE_NAME_USER_CONFIG, newTableVersionCode);
// }
}
printData();
Log.d(TAG, "method:initUserConfig#newTableVersionCode < lastTableVersionCode");
Log.d(TAG, "method:initUserConfig#out");
}
private void printData() {
Log.d(TAG, "method:printData#in");
List<TableVersionEntity> tableVersionList = TableVersionManager.getInstance().queryAllTableVersion();
Log.d(TAG, "method:printData#tableVersionList=" + tableVersionList);
long count = UserConfigManager.getInstance().queryCountUserConfig();
Log.d(TAG, "method:printData#count=" + count);
List<UserConfigEntity> userConfigList = UserConfigManager.getInstance().queryAllUserConfig();
Log.d(TAG, "method:printData#userConfigList=" + userConfigList);
Log.d(TAG, "method:printData#out");
}
/**
* 根据字段的操作类型调用相应的修改、删除等操作
*
* @param userConfigEntity
*/
private long handleUserConfigByOperationType(UserConfigEntity userConfigEntity) {
Log.d(TAG, "method:handleUserConfiByType#in#arguments:newUserConfig=" + userConfigEntity);
// 该字段的操作类型
int operationType = userConfigEntity.getOperationType();
if (operationType == Constants.USER_CONFIG_FIELD_OPERATION_TYPE_INSERT_OR_UPDATE) {
long rowId = UserConfigManager.getInstance().insertOrReplaceUserConfig(userConfigEntity);
Log.d(TAG, "method:handleUserConfiByType#rowId=" + rowId);
return rowId;
}
if (operationType == Constants.USER_CONFIG_FIELD_OPERATION_TYPE_DELETE) {
String pkey = userConfigEntity.getPkey();
long removeRowId = UserConfigManager.getInstance().deleteUserConfigByKey(pkey);
Log.d(TAG, "method:handleUserConfiByType#removeRowId=" + removeRowId);
return removeRowId;
}
Log.d(TAG, "method:handleUserConfiByType#out#return value:0");
return 0;
}
/**
* 修改本地数据库中表对应的版本号
*
* @return
*/
private void updateTableVersion(long id, String tableName, int versionCode) {
Log.d(TAG, "method:updateTableVersion#in#arguments:tableName=" + tableName + ", versionCode=" + versionCode);
TableVersionEntity tableVersion = new TableVersionEntity(id, tableName, versionCode);
TableVersionManager.getInstance().insertOrReplaceTableVersion(tableVersion);
Log.d(TAG, "method:updateTableVersion#out");
}
/**
* 解析出json文件中的所有最新的配置相关的数据
*
* @return
*/
public List<UserConfigEntity> parseNewUserConfigList() {
Log.d(TAG, "method:parseNewUserConfigList#in");
List<UserConfigEntity> userConfigList = null;
Map<String, Integer> version = null;
try {
String jsonStr = ResUtils.getJsonStr(mContext, "user_config.json");
Log.d(TAG, "method:parseNewUserConfigList#jsonStr=" + jsonStr);
Gson gson = new Gson();
userConfigList = gson.fromJson(jsonStr, new TypeToken<List<UserConfigEntity>>() {
}.getType());
} catch (Exception e) {
e.printStackTrace();
}
Log.d(TAG, "method:parseNewUserConfigList#out#return value:userConfigList=" + userConfigList);
return userConfigList;
}
/**
* 解析出json文件中的所有最新的表相关的数据
*
* @return
*/
private Map<String, Integer> parseNewTableVersionMap() {
Log.d(TAG, "method:parseNewTableVersionMap#in");
Map<String, Integer> newTableVersionMap = null;
try {
String jsonStr = ResUtils.getJsonStr(mContext, "table_version.json");
Log.d(TAG, "method:parseNewTableVersionMap#jsonStr=" + jsonStr);
Gson gson = new Gson();
newTableVersionMap = gson.fromJson(jsonStr, new TypeToken<Map<String, Integer>>() {
}.getType());
} catch (Exception e) {
e.printStackTrace();
}
Log.d(TAG, "method:parseNewTableVersionMap#out#return value:newTableVersionMap=" + newTableVersionMap);
return newTableVersionMap;
}
}
复制代码
上記が主な実装プロセスです。構成データを変更するには、契約に従ってjson文字列ファイルを変更するだけです。データベースに対応する運用ビジネスは通常変更する必要はありません。コードコメントの例は次のように記述されています。詳細であり、特定のロジックの実装は繰り返しではありません。
作者のレベルが限られているため、言語の説明やコードの実装には必然的に欠陥があります。もっと貴重な意見をいただければ幸いです。
こんにちは世界 !
全てに感謝!
この記事は、ナゲット作成の道を一緒に始めるための「新人作成セレモニー」イベントに参加しました