Android本地数据库的选择ObjectBox,Realm

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/f552126367/article/details/88527989

一、前言

本来不太想写这篇博客的,因为网络上关于Android数据的对比博客已经挺多了,以前自己用过OrmLite,最近接手了两个项目,一个用的是Realm,一个用的是ObjectBox,感觉如果自己不写个博客,过段时间忘了就糟糕了,趁着现在刚写完,记录一下。

二、数据库比较

1)有db数据库的:如OrmLite(自己用过),GreenDao(流行)这两个,都会在Android本地建立Sqlite数据库,我自己用过的OrmLite写起来比较复杂,而且效率是真慢,GreenDao这个效率相对来说还可以。

2)无Sqlite数据库的:流行的就Realm和最近新出的ObjectBox,他会建立自己的搜索引擎。在本地文件中无法直接查看数据库数据,但是要比有db的效率高很多,要不然何必多费事自己建立数据库引擎呢。

三、效率对比

总体来说,ObjectBox要比Realm快2-3倍,但是对于数据较少的操作,差距不大,因为Realm已经很快了。

四、ObjectBox代码实现

 1、在build下引入ObjectBox

 ext.objectboxVersion = '2.3.3'
classpath "io.objectbox:objectbox-gradle-plugin:$objectboxVersion"

2、在App的build下引入ObjectBox包

//ObjectBox数据库
apply plugin: 'io.objectbox'   

 implementation "io.objectbox:objectbox-java:$objectboxVersion"
    annotationProcessor "io.objectbox:objectbox-processor:$objectboxVersion"

3、在Application.xml中引入ObjectBox

private static BoxStore mBoxStore;
 mBoxStore = MyObjectBox.builder().androidContext(this).build();
    public static BoxStore getBoxStore() {
        return mBoxStore;
    }

4、创建对象bean,bean中必须有@Id(主键),而且必须是long类型,这个主键可以不是我们自己的主键,是ObjectBox自己自动生成的。如果有自己的主键可以额外定义

@Entity
public class BoxBean {
    //@Id(assignable = true)表示自己
    @Id
    public Long uuid;
    public String name;
    public Date date;

    public Long getUuid() {
        return uuid;
    }

    public void setUuid(Long uuid) {
        this.uuid = uuid;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }
}

5、使用增加和删除方法

private  Box<BoxBean>  boxBean;
boxBean=((MyApplication)getApplication()).getBoxStore().boxFor(BoxBean.class);

  //保存数据
    private void saveData(){
        BoxBean bean=new BoxBean();
        bean.setName(realm_name.getText().toString());
        bean.setDate(Helper.getNowDate());
        boxBean.put(bean);
    }

 //删除数据
                boxBean.remove(item.get(i));

五、Reaml数据库

1、在build中调用

       //Realm数据库
        classpath "io.realm:realm-gradle-plugin:5.9.1"

2、在APP的build中调用

//Realm数据库加载
apply plugin: 'realm-android'

3、在Application.xml中引入Realm

    private void initRealm(){
       // Realm 文件将创建在 Context.getFilesDir() 目录下,名字为 "myrealm.realm"
        Realm.init(this);
        RealmConfiguration config = new RealmConfiguration.Builder()
                .name("myrealm.realm")
//                .encryptionKey(getKey())
                .schemaVersion(3)//如果版本不同则会删库升级
                .deleteRealmIfMigrationNeeded()
//                .modules(new MySchemaModule())
                //指定迁移操作的迁移类。
//                .migration(new MyMigration())
                .build();
        Realm.setDefaultConfiguration(config);
    }

4、创建实体bean

public class Person extends RealmObject {
    @PrimaryKey
    private String personId;//"@PrimaryKey"声明该属性类似于主键

    private String personName;
    private Integer personage;

    public String getPersonId() {
        return personId;
    }

    public void setPersonId(String personId) {
        this.personId = personId;
    }

    public String getPersonName() {
        return personName;
    }

    public void setPersonName(String personName) {
        this.personName = personName;
    }

    public Integer getPersonage() {
        return personage;
    }

    public void setPersonage(Integer personage) {
        this.personage = personage;
    }
}

5、使用代码

private Realm realm;

realm=Realm.getDefaultInstance();

    //保存数据
    private void saveData(){
            realm=Realm.getDefaultInstance();
            realm.beginTransaction();//开启事务
            Person person=realm.createObject(Person.class,Helper.getUUID());
            person.setPersonName(realm_name.getText().toString());
            person.setPersonage(0);
            realm.commitTransaction();
    }
 //删除数据
                realm.beginTransaction();//开启事务
                personList.get(i).deleteFromRealm();
                realm.commitTransaction();

    @Override
    protected void onDestroy() {
        super.onDestroy();
        realm.close();
    }

六、总结

1、对于具体数据库的选择,我觉得只需要在GreenDao,Realm,ObjectBox中选取就可以了,如果想在自己的Android手机中建立自己的db数据库,就用GreenDao,但我感觉这个需求并不是很需要

2、而对于无SqlLite的数据库,我觉得Realm和ObjectBox比较来说,还是ObjectBox要好,无论是性能还是难易性来说,ObjectBox都要比Realm好,而且ObjectBox是最近出的,在市场上已经有Realm的情况下,还敢出类似功能的数据库,肯定是有他的优势的。但是Realm数据库对一般操作也都能满足,所以如何选择还是看自己的了解程度了。

猜你喜欢

转载自blog.csdn.net/f552126367/article/details/88527989