Realm数据库使用教程(七):数据库加密和Realm使用注意事项

来源:互联网 发布:php gd库 压缩图片 编辑:程序博客网 时间:2024/06/05 04:39

Realm数据库使用教程(六):数据迁移

数据库加密

Realm自带数据库加密,需要64位字节数据进行加密。

  • 官方原文

    Realm 文件可以通过传递一个512位(64字节)的密钥参数给 Realm.getInstance().encryptionKey() 来加密存储在磁盘上。
byte[] key = new byte[64];new SecureRandom().nextBytes(key);RealmConfiguration config = new RealmConfiguration.Builder()  .encryptionKey(key)  .build();Realm realm = Realm.getInstance(config);
保证了所有永久性存储在磁盘上的数据都是通过标准 AES-256 加密的。每次创建新的 Realm 实例的时候,都需要提供相同的密钥。
  • 我的做法

因为每次创建新的 Realm 实例的时候,都需要提供相同的密钥,所以如果使用官方随机产生的话,第二次打开app数据库可能会禁止访问!

创建一个16字节的key

private static String key = "huangxiaoguo1234";

转换为64位

  /**     * 获取Realm数据库64位秘钥     *     * @param key     * @return     */    public static byte[] getRealmKey(String key) {        String newKey = "";        for (int i = 0; i < 4; i++) {            newKey = newKey + key;        }        return newKey.getBytes();    }

指定数据库的密钥

package tsou.com.simple.realmtest;import android.app.Application;import android.content.Context;import com.facebook.stetho.Stetho;import com.uphyca.stetho_realm.RealmInspectorModulesProvider;import java.security.SecureRandom;import io.realm.Realm;import io.realm.RealmConfiguration;import tsou.com.simple.realmtest.migration.CustomMigration;import tsou.com.simple.realmtest.utils.UIUtils;/** * Created by Administrator on 2017/12/15 0015. */public class MyApplication extends Application {    /**     * 上下文     */    private static MyApplication instance;    private static RealmConfiguration config;    private static String key = "huangxiaoguo1234";    @Override    public void onCreate() {        super.onCreate();        /**         * 在Realm中Stetho需要配置         */        Stetho.initialize(                Stetho.newInitializerBuilder(this)                        .enableDumpapp(Stetho.defaultDumperPluginsProvider(this))                        .enableWebKitInspector(RealmInspectorModulesProvider.builder(this).build())                        .build());        Realm.init(this);        instance = this;        new SecureRandom().nextBytes(UIUtils.getRealmKey(key));        config = new RealmConfiguration.Builder()                .name("huangxiaoguo.realm")//指定数据库的名称。如不指定默认名为default。                .encryptionKey(UIUtils.getRealmKey(key))//指定数据库的密钥。                .schemaVersion(1)//                .deleteRealmIfMigrationNeeded()//声明版本冲突时自动删除原数据库,开发时候打开                .migration(new CustomMigration())//指定迁移操作的迁移类。//                .inMemory()// 声明数据库只在内存中持久化                .build();//        mRealm = Realm.getDefaultInstance();//        mRealm = Realm.getInstance(config);    }    public static Context getInstance() {        return instance;    }    public static RealmConfiguration getRealmConfiguration() {        return config;    }}

数据库加密完成!


Realm使用注意事项

  • 线程限制

eg:

异步删除:先查找到数据(无效)

 //失败(原因是因为线程限制)                final RealmResults<Student> students4 = mRealm.where(Student.class).findAll();                realmAsyncTask = mRealm.executeTransactionAsync(new Realm.Transaction() {                    @Override                    public void execute(Realm realm) {                        students4.deleteFromRealm(0);                        students4.deleteFirstFromRealm();                        students4.deleteLastFromRealm();                        students4.deleteAllFromRealm();                    }                }, new Realm.Transaction.OnSuccess() {                    @Override                    public void onSuccess() {                        UIUtils.showToast("删除成功");                    }                }, new Realm.Transaction.OnError() {                    @Override                    public void onError(Throwable error) {                        UIUtils.showToast("删除失败");                    }                });

deleteAll()(崩溃)

//崩溃(原因是因为线程限制) mRealm.deleteAll();

delete(xxx.class)(崩溃)

//崩溃(原因是因为线程限制)mRealm.delete(Student.class);

Intent:传递对象(崩溃)

这种做法是不允许的,即使你User实现了Serializable接口RealmResults<User> users = mRealm.where(User.class).findAll();                if (users.size() > 0) {                    User user = users.get(0);                    Intent intent = new Intent(this, TestActivity.class);                    intent.putExtra("user", user);                    startActivity(intent);                }else {                    UIUtils.showToast("数据库没有数据");                }

以上做法均是Realm不允许的做法!

一、 RealmObject自带线程保护功能,只能在创建它的线程中访问,在子线程中不能访问。
也就是说,如果你在主线程中new了一个RealmObject对象 user,那么在子线程中是访问不了user对象的。
要想在子线程中访问,必须先将user存入Ream中,然后在子线程中query出来。

二、 如果Realm关闭,所有查询得到的RealmObject都不能使用了。
如果想在子线程中去查询数据,然后在主线程中使用是无法做到的。所以Realm提供的异步查询就很重要了…

三、如果想在Realm.close()之后继续操作,需要查询得到的对象

四、如果直接修改或删除query得到的数据,必须在transaction中完成…
也就是说,你根本不能把query返回的对象,当成普通对象去赋值或删除,如果想要直接操作…,把对象copy一份传出来…

  • Intent:传递主键(重要官方建议)
 //你不可以直接通过 intent传递 RealmObject,建议你只传递RealmObject的标识符。                RealmResults<User> all = mRealm.where(User.class).findAll();                if (all.size() > 0) {                    int id = all.get(0).getId();                    Intent intent = new Intent(this, TestActivity.class);                    intent.putExtra("id", id);                    startActivity(intent);                }else {                    UIUtils.showToast("数据库没有数据");                }

然后在下个页面重新查询数据库

mRealm = UIUtils.getRealmInstance();
 int id = getIntent().getIntExtra("id", -1);        if (id != -1) {            user = mRealm.where(User.class).equalTo("id", id).findFirst();        }
 if (user != null) {            mText.setText("id=" + user.getId() +                    ",name=" + user.getName() + ",age="                    + user.getAge() + ",sex=" + user.getSex());        }
  • Realm支持Rxjava,但是不太稳定,版本更改后会有部分方法进行替换了,如果有需要可以去尝试!

作者对Realm的使用介绍到此就告一段落了!有什么不妥之处,望指出,谢谢

Demo地址:https://gitee.com/huangxiaoguo/Realm