这是我参与8月更文挑战的第27天,活动详情查看:8月更文挑战
介绍
如果要保存的相对较小键值对集合,一般我们会使用使用 SharedPreferencesAPI,现在多了一个选择MMKV。
SharedPreferences
获取SharedPreferences实例
public abstract SharedPreferences getSharedPreferences(String name, @PreferencesMode int mode);
private SharedPreferences getSP(){
SharedPreferences sharedPref = null;
if(sharedPref == null){
Context context = getApplicationContext();
sharedPref = context.getSharedPreferences(getPackageName(),Context.MODE_PRIVATE);
}
return sharedPref;
}
- name 文件名
- mode
@IntDef(flag = true, prefix = { "MODE_" }, value = {
MODE_PRIVATE,/*默认*/
MODE_WORLD_READABLE,/*已废弃*/
MODE_WORLD_WRITEABLE,/*已废弃*/
MODE_MULTI_PROCESS,/*已废弃*/
})
@Retention(RetentionPolicy.SOURCE)
public @interface PreferencesMode {}
写入数据
private void writeData(){
SharedPreferences sp = getSP();
SharedPreferences.Editor edit = sp.edit();
edit.putInt("key",2021);
//boolean commit = edit.commit();
edit.apply();
}
apply() 会立即更改内存中的 SharedPreferences 对象,但会将更新异步写入磁盘。或者,您也可以使用 commit() 将数据同步写入磁盘。但是,由于 commit() 是同步的,避免从主线程调用它,因为它可能会导致UI线程block。
读取数据
private int readData(){
SharedPreferences sp = getSP();
int key = sp.getInt("key", 0);
return key;
}
MMKV
MMKV 是基于 mmap 内存映射的 key-value 组件,底层序列化/反序列化使用 protobuf 实现,性能高,稳定性强。从 2015 年中至今,在 iOS 微信上使用,其性能和稳定性经过了时间的验证。也已移植到 Android 平台,一并开源。
基本要求
- MMKV 支持 API level 16 以上平台;
- MMKV 需使用 NDK r16b 或以上进行编译 (通过源码引入 MMKV 的话)
安装引入
通过 Maven
Gradle 在编译工程的时候会自动从 maven 仓库下载 AAR 包。
implementation 'com.tencent:mmkv-static:1.2.10'
基本使用
MMKV 的使用非常简单,所有变更立马生效,无需调用 sync、apply。
配置 MMKV 根目录
在 App 启动时初始化 MMKV,设定 MMKV 的根目录(files/mmkv/),例如在 onCreate() 里:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mmkvactivity);
String rootDir = MMKV.initialize(this);
Log.d(TAG, "onCreate: MMKV paych = " + rootDir);
}
CRUD
private void crud(){
MMKV kv = MMKV.defaultMMKV();
kv.encode("bool", true);
Log.d(TAG, "crud: " + "bool: " + kv.decodeBool("bool"));
kv.encode("int", Integer.MIN_VALUE);
Log.d(TAG, "crud: " + "int: " + kv.decodeInt("int"));
kv.encode("long", Long.MAX_VALUE);
Log.d(TAG, "crud: " + "long: " + kv.decodeLong("long"));
kv.encode("float", -3.14f);
Log.d(TAG, "crud: " + "float: " + kv.decodeFloat("float"));
kv.encode("double", Double.MIN_VALUE);
Log.d(TAG, "crud: " + "double: " + kv.decodeDouble("double"));
kv.encode("string", "Hello from mmkv");
Log.d(TAG, "crud: " + "string: " + kv.decodeString("string"));
byte[] bytes = {'m', 'm', 'k', 'v'};
kv.encode("bytes", bytes);
Log.d(TAG, "crud: " + "bytes: " + new String(kv.decodeBytes("bytes")));
}
删除& 查询
private void removeAndQuiry(){
MMKV kv = MMKV.defaultMMKV();
Log.d(TAG, "removeAndQuiry: ===============================================");
kv.removeValueForKey("bool");
Log.d(TAG, "removeAndQuiry: " + "bool: " + kv.decodeBool("bool"));
kv.removeValuesForKeys(new String[]{"int", "long"});
Log.d(TAG, "removeAndQuiry: " + "allKeys: " + Arrays.toString(kv.allKeys()));
boolean hasBool = kv.containsKey("bool");
Log.d(TAG, "removeAndQuiry: has bool ? " + hasBool);
}
-
如果不同业务需要区别存储,也可以单独创建自己的实例:
MMKV kv = MMKV.mmkvWithID("MyID"); kv.encode("bool", true); -
如果业务需要多进程访问,那么在初始化的时候加上标志位
MMKV.MULTI_PROCESS_MODE:MMKV kv = MMKV.mmkvWithID("InterProcessKV", MMKV.MULTI_PROCESS_MODE); kv.encode("bool", true);