Realm数据库使用教程(一),从外包公司到今日头条offer

分别采用greendao、ormlite、realm三种数据框架对同一数据结构Note进行10000次和1000次的增、查、删操作,对比其各自的运行效率,其运行速度对比具体如下表所示(时间单位为毫秒):

这里写图片描述

上面数据仅表示在增、查、删操作时运行速度的快慢,其他性能、功能、未做对比,对比结果显著,不做详细赘。

数据来自:GreenDao、Ormlite、Realm性能对比

  • 引入Realm

在你项目的build.gradle中:

dependencies {

classpath ‘com.android.tools.build:gradle:3.0.0-beta6’

// classpath “io.realm:realm-gradle-plugin:4.3.1”

//stetho_realm不支持高版本,这里使用3.0.0

classpath “io.realm:realm-gradle-plugin:3.0.0”

}

在你app的build.gradle中:

apply plugin: ‘realm-android’

realm的官方最新版本为4.3.1,但是为了兼容stetho我将版本更改为3.0.0

  • 引入stetho

在你项目的build.gradle中:

repositories {

maven {

url ‘https://github.com/uPhyca/stetho-realm/raw/master/maven-repo’

}

}

在你app的build.gradle中:

compile ‘com.facebook.stetho:stetho:1.5.0’

compile ‘com.uphyca:stetho_realm:2.1.0’

stetho_realm的github地址:https://github.com/uPhyca/stetho-realm

  • 初始化

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。

.schemaVersion(0)

.deleteRealmIfMigrationNeeded()//声明版本冲突时自动删除原数据库,开发时候打开

// .inMemory()// 声明数据库只在内存中持久化

.build();

}

public static Context getInstance() {

return instance;

}

public static RealmConfiguration getRealmConfiguration() {

return config;

}

}

RealmConfiguration支持的方法:

  1. Builder.name : 指定数据库的名称。如不指定默认名为default。

  2. Builder.schemaVersion : 指定数据库的版本号。

  3. Builder.encryptionKey : 指定数据库的密钥。

  4. Builder.migration : 指定迁移操作的迁移类。

  5. Builder.deleteRealmIfMigrationNeeded : 声明版本冲突时自动删除原数据库。

  6. Builder.inMemory : 声明数据库只在内存中持久化。

  7. build : 完成配置构建。


  • 得到Realm对象,打开数据库操作

package tsou.com.simple.realmtest.utils;

import android.content.Context;

import android.content.res.AssetManager;

import android.widget.Toast;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import io.realm.Realm;

import tsou.com.simple.realmtest.MyApplication;

public class UIUtils {

private static Toast toast;

/**

  • 静态吐司

  • @param context

  • @param text

*/

public static void showToast(Context context, String text) {

if (toast == null) {

toast = Toast.makeText(context, text, Toast.LENGTH_SHORT);

}

toast.setText(text);

toast.show();

}

/**

  • 不需要上下文对象的 静态toast

*/

public static void showToast(String text) {

showToast(getContext(), text);

}

/**

  • 获取上下文对象

  • @return

*/

public static Context getContext() {

return MyApplication.getInstance();

}

/**

  • 获得Realm实例

  • @return

*/

public static Realm getRealmInstance() {

return Realm.getInstance(MyApplication.getRealmConfiguration());

}

/**

  • 获取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();

}

/**

  • 从asset路径下读取对应文件转String输出

  • @return

*/

public static String getJson(String fileName) {

StringBuilder sb = new StringBuilder();

AssetManager am = getContext().getAssets();

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级安卓工程师,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年最新Android移动开发全套学习资料》送给大家,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
img
img
img
img

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频
如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注Android)
img

自学效果低效又漫长,而且极易碰到天花板技术停滞不前!**

因此收集整理了一份《2024年最新Android移动开发全套学习资料》送给大家,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
[外链图片转存中…(img-AW1ZbhaH-1710928457433)]
[外链图片转存中…(img-wfZ0p6Wa-1710928457434)]
[外链图片转存中…(img-edbaWHnx-1710928457434)]
[外链图片转存中…(img-KeRpMsw8-1710928457435)]

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频
如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注Android)
[外链图片转存中…(img-xCroPvP3-1710928457435)]

  • 9
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值