Android主流三方库源码分析(4、深刻理解GreenDao源码)

前言

成为一名优秀的Android开发,须要一份完备的知识体系,在这里,让咱们一块儿成长为本身所想的那样~。

前两篇咱们详细地分析了Android的网络底层框架OKHttp和封装框架Retrofit的核心源码,若是对OKHttp或Retrofit内部机制不了解的能够看看Android主流三方库源码分析(1、深刻理解OKHttp源码)Android主流三方库源码分析(2、深刻理解Retrofit源码),除了热门的网络库以外,咱们还分析了使用最普遍的图片加载框架Glide的加载流程,你们读完这篇源码分析实力会有很多提高,有兴趣能够看看Android主流三方库源码分析(3、深刻理解Glide源码)。本篇,咱们将会来对目前Android数据库框架中性能最好的GreenDao来进行较为深刻地讲解。html

1、基本使用流程

一、导入GreenDao的代码生成插件和库

// 项目下的build.gradle
buildscript {
    ...
    dependencies {
        classpath 'com.android.tools.build:gradle:2.3.0'
        classpath 'org.greenrobot:greendao-gradle-plugin:3.2.1' 
    }
}

// app模块下的build.gradle
apply plugin: 'com.android.application'
apply plugin: 'org.greenrobot.greendao'

...

dependencies {
    ...
    compile 'org.greenrobot:greendao:3.2.0' 
}
复制代码

二、建立一个实体类,这里为HistoryData

@Entity
public class HistoryData {

    @Id(autoincrement = true)
    private Long id;

    private long date;

    private String data;
}
复制代码

三、选择ReBuild Project,HistoryData会被自动添加Set/get方法,并生成整个项目的DaoMaster、DaoSession类,以及与该实体HistoryData对应的HistoryDataDao。

image

@Entity
public class HistoryData {

    @Id(autoincrement = true)
    private Long id;

    private long date;

    private String data;

    @Generated(hash = 1371145256)
    public HistoryData(Long id, long date, String data) {
        this.id = id;
        this.date = date;
        this.data = data;
    }

    @Generated(hash = 422767273)
    public HistoryData() {
    }

    public Long getId() {
        return this.id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public long getDate() {
        return this.date;
    }

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

    public String getData() {
        return this.data;
    }

    public void setData(String data) {
        this.data = data;
    }
}
复制代码

这里点明一下这几个类的做用:android

  • DaoMaster:全部Dao类的主人,负责整个库的运行,内部的静态抽象子类DevOpenHelper继承并重写了Android的SqliteOpenHelper。
  • DaoSession:做为一个会话层的角色,用于生成相应的Dao对象、Dao对象的注册,操做Dao的具体对象。
  • xxDao(HistoryDataDao):生成的Dao对象,用于进行具体的数据库操做。

四、获取并使用相应的Dao对象进行增删改查操做

DaoMaster.DevOpenHelper devOpenHelper = new DaoMaster.DevOpenHelper(this, Constants.DB_NAME);
SQLiteDatabase database = devOpenHelper.getWritableDatabase();
DaoMaster daoMaster = new DaoMaster(database);
mDaoSession = daoMaster.newSession();
HistoryDataDao historyDataDao = daoSession.getHistoryDataDao();

// 省略建立historyData的代码
...

// 增
historyDataDao.insert(historyData);

// 删
historyDataDao.delete(historyData);

// 改
historyDataDao.update(historyData);

// 查
List<HistoryData> historyDataList = historyDataDao.loadAll();
复制代码

本篇文章将会以上述使用流程来对GreenDao的源码进行逐步分析,最后会分析下GreenDao中一些优秀的特性,让读者朋友们对GreenDao的理解有更一步的加深。git

2、GreenDao使用流程分析

一、建立数据库帮助类对象DaoMaster.DevOpenHelper

DaoMaster.DevOpenHelper devOpenHelper = new DaoMaster.DevOpenHelper(this, Constants.DB_NAME);
复制代码

建立GreenDao内部实现的数据库帮助类对象devOpenHelper,核心源码以下:程序员

public class DaoMaster extends AbstractDaoMaster {

    ...

    public static abstract class OpenHelper extends DatabaseOpenHelper {
    
    ...
    
         @Override
        public void onCreate(Database db) {
            Log.i("greenDAO", "Creating tables for schema version " + SCHEMA_VERSION);
            createAllTables(db, false);
        }
    }
    
    public static class DevOpenHelper extends OpenHelper {
    
        ...
        
        @Override
        public void onUpgrade(Database db, int oldVersion, int newVersion) {
            Log.i("greenDAO", "Upgrading schema from version " + oldVersion + " to " + newVersion + " by dropping all tables");
            dropAllTables(db, true);
            onCreate(db);
        }
    }
}
复制代码

DevOpenHelper自身实现了更新的逻辑,这里是弃置了全部的表,而且调用了OpenHelper实现的onCreate方法用于建立全部的表,其中DevOpenHelper继承于OpenHelper,而OpenHelper自身又继承于DatabaseOpenHelper,那么,这个DatabaseOpenHelper这个类的做用是什么呢?github

public abstract class DatabaseOpenHelper extends SQLiteOpenHelper {

    ...
    
    // 关注点1
    public Database getWritableDb() {
        return wrap(getWritableDatabase());
    }
    
    public Database getReadableDb() {
        return wrap(getReadableDatabase());
    }   
    
    protected Database wrap(SQLiteDatabase sqLiteDatabase) {
        return new StandardDatabase(sqLiteDatabase);
    }
    
    ...
    
    // 关注点2
    public Database getEncryptedWritableDb(String password) {
        EncryptedHelper encryptedHelper = checkEncryptedHelper();
        return encryptedHelper.wrap(encryptedHelper.getWritableDatabase(password));
    }
    
    public Database getEncryptedReadableDb(String password) {
        EncryptedHelper encryptedHelper = checkEncryptedHelper();
        return encryptedHelper.wrap(encryptedHelper.getReadableDatabase(password));
    }
    
    ...
    
    private class EncryptedHelper extends net.sqlcipher.database.SQLiteOpenHelper {
    
        ...
    
    
        protected Database wrap(net.sqlcipher.database.SQLiteDatabase     sqLiteDatabase) {
            return new EncryptedDatabase(sqLiteDatabase);
        }
    }
复制代码

其实,DatabaseOpenHelper也是实现了SQLiteOpenHelper的一个帮助类,它内部能够获取到两种不一样的数据库类型,一种是标准型的数据库StandardDatabase,另外一种是加密型的数据库EncryptedDatabase,从以上源码可知,它们内部都经过wrap这样一个包装的方法,返回了对应的数据库类型,咱们大体看一下StandardDatabase和EncryptedDatabase的内部实现。sql

public class StandardDatabase implements Database {

    // 这里的SQLiteDatabase是android.database.sqlite.SQLiteDatabase包下的
    private final SQLiteDatabase delegate;

    public StandardDatabase(SQLiteDatabase delegate) {
        this.delegate = delegate;
    }

    @Override
    public Cursor rawQuery(String sql, String[] selectionArgs) {
        return delegate.rawQuery(sql, selectionArgs);
    }

    @Override
    public void execSQL(String sql) throws SQLException {
        delegate.execSQL(sql);
    }

    ...
}

public class EncryptedDatabaseStatement implements DatabaseStatement     {

    // 这里的SQLiteStatement是net.sqlcipher.database.SQLiteStatement包下的
    private final SQLiteStatement delegate;

    public EncryptedDatabaseStatement(SQLiteStatement delegate) {
        this.delegate = delegate;
    }

    @Override
    public void execute() {
        delegate.execute();
    }
    
    ...
}
复制代码

StandardDatabase和EncryptedDatabase这两个类内部都使用了代理模式给相同的接口添加了不一样的具体实现,StandardDatabase天然是使用的Android包下的SQLiteDatabase,而EncryptedDatabaseStatement为了实现加密数据库的功能,则使用了一个叫作sqlcipher的数据库加密三方库,若是你项目下的数据库须要保存比较重要的数据,则可使用getEncryptedWritableDb方法来代替getdWritableDb方法对数据库进行加密,这样,咱们以后的数据库操做则会以代理模式的形式间接地使用sqlcipher提供的API去操做数据库数据库

二、建立DaoMaster对象

SQLiteDatabase database = devOpenHelper.getWritableDatabase();
DaoMaster daoMaster = new DaoMaster(database);
复制代码

首先,DaoMaster做为全部Dao对象的主人,它内部确定是须要一个SQLiteDatabase对象的,所以,先由DaoMaster的帮助类对象devOpenHelper的getWritableDatabase方法获得一个标准的数据库类对象database,再由此建立一个DaoMaster对象。json

public class DaoMaster extends AbstractDaoMaster {

    ...

    public DaoMaster(SQLiteDatabase db) {
        this(new StandardDatabase(db));
    }

    public DaoMaster(Database db) {
        super(db, SCHEMA_VERSION);
        registerDaoClass(HistoryDataDao.class);
    }
    
    ...
}
复制代码

在DaoMaster的构造方法中,它首先执行了super(db, SCHEMA_VERSION)方法,即它的父类AbstractDaoMaster的构造方法。缓存

public abstract class AbstractDaoMaster {

    ...

    public AbstractDaoMaster(Database db, int schemaVersion) {
        this.db = db;
        this.schemaVersion = schemaVersion;

        daoConfigMap = new HashMap<Class<? extends AbstractDao<?, ?>>, DaoConfig>();
    }
    
    protected void registerDaoClass(Class<? extends AbstractDao<?, ?>> daoClass) {
        DaoConfig daoConfig = new DaoConfig(db, daoClass);
        daoConfigMap.put(daoClass, daoConfig);
    }
    
    ...
}
复制代码

在AbstractDaoMaster对象的构造方法中,除了记录当前的数据库对象db和版本schemaVersion以外,还建立了一个类型为HashMap<Class>, DaoConfig>()的daoConfigMap对象用于保存每个DAO对应的数据配置对象DaoConfig,而且Daoconfig对象存储了对应的Dao对象所必需的数据。最后,在DaoMaster的构造方法中使用了registerDaoClass(HistoryDataDao.class)方法将HistoryDataDao类对象进行了注册,实际上,就是为HistoryDataDao这个Dao对象建立了相应的DaoConfig对象并将它放入daoConfigMap对象中保存起来。安全

三、建立DaoSession对象

mDaoSession = daoMaster.newSession();
复制代码

在DaoMaster对象中使用了newSession方法新建了一个DaoSession对象。

public DaoSession newSession() {
    return new DaoSession(db, IdentityScopeType.Session, daoConfigMap);
}
复制代码

在DaoSeesion的构造方法中,又作了哪些事情呢?

public class DaoSession extends AbstractDaoSession {

    ...

    public DaoSession(Database db, IdentityScopeType type, Map<Class<?     extends AbstractDao<?, ?>>, DaoConfig>
            daoConfigMap) {
        super(db);

        historyDataDaoConfig = daoConfigMap.get(HistoryDataDao.class).clone();
        historyDataDaoConfig.initIdentityScope(type);

        historyDataDao = new HistoryDataDao(historyDataDaoConfig, this);

        registerDao(HistoryData.class, historyDataDao);
    }
    
    ...
}
复制代码

首先,调用了父类AbstractDaoSession的构造方法。

public class AbstractDaoSession {

    ...

    public AbstractDaoSession(Database db) {
        this.db = db;
        this.entityToDao = new HashMap<Class<?>, AbstractDao<?, ?>>();
    }
    
    protected <T> void registerDao(Class<T> entityClass, AbstractDao<T, ?> dao) {
        entityToDao.put(entityClass, dao);
    }
    
    ...
}
复制代码

在AbstractDaoSession构造方法里面建立了一个实体与Dao对象的映射集合。接下来,在DaoSession的构造方法中还作了2件事:

  • 一、建立每个Dao对应的DaoConfig对象,这里是historyDataDaoConfig,而且根据IdentityScopeType的类型初始化建立一个相应的IdentityScope,根据type的不一样,它有两种类型,分别是IdentityScopeObjectIdentityScopeLong,它的做用是根据主键缓存对应的实体数据。当主键是数字类型的时候,如long/Long、int/Integer、short/Short、byte/Byte,则使用IdentityScopeLong缓存实体数据,当主键不是数字类型的时候,则使用IdentityScopeObject缓存实体数据。
  • 二、根据DaoSession对象和每个Dao对应的DaoConfig对象,建立与之对应的historyDataDao对象,因为这个项目只建立了一个实体类HistoryData,所以这里只有一个Dao对象historyDataDao,而后就是注册Dao对象,其实就是将实体和对应的Dao对象放入entityToDao这个映射集合中保存起来了。

四、插入源码分析

HistoryDataDao historyDataDao = daoSession.getHistoryDataDao();

// 增
historyDataDao.insert(historyData);
复制代码

这里首先在会话层DaoSession中获取了咱们要操做的Dao对象HistoryDataDao,而后插入了一个咱们预先建立好的historyData实体对象。其中HistoryDataDao继承了AbstractDao<HistoryData, Long> 。

public class HistoryDataDao extends AbstractDao<HistoryData, Long> {
    ...
}
复制代码

那么,这个AbstractDao是干什么的呢?

public abstract class AbstractDao<T, K> {

    ...
    
    public List<T> loadAll() {
        Cursor cursor = db.rawQuery(statements.getSelectAll(), null);
        return loadAllAndCloseCursor(cursor);
    }
    
    ...
    
    public long insert(T entity) {
        return executeInsert(entity, statements.getInsertStatement(),     true);
    }
    
    ...
    
    public void delete(T entity) {
        assertSinglePk();
        K key = getKeyVerified(entity);
        deleteByKey(key);
    }
    
    ...

}
复制代码

看到这里,根据程序员优秀的直觉,你们应该能猜到,AbstractDao是全部Dao对象的基类,它实现了实体数据的操做如增删改查。咱们接着分析insert是如何实现的,在AbstractDao的insert方法中又调用了executeInsert这个方法。在这个方法中,第二个参里的statements是一个TableStatements对象,它是在AbstractDao初始化构造器时从DaoConfig对象中取出来的,是一个根据指定的表格建立SQL语句的一个帮助类。使用statements.getInsertStatement()则是获取了一个插入的语句。而第三个参数则是判断是不是主键的标志。

public class TableStatements {

    ...

    public DatabaseStatement getInsertStatement() {
        if (insertStatement == null) {
            String sql = SqlUtils.createSqlInsert("INSERT INTO ", tablename, allColumns);
            DatabaseStatement newInsertStatement = db.compileStatement(sql);
            ...
        }
        return insertStatement;
    }

    ...
}
复制代码

在TableStatements的getInsertStatement方法中,主要作了两件事:

  • 一、使用SqlUtils建立了插入的sql语句
  • 二、根据不一样的数据库类型(标准数据库或加密数据库)将sql语句编译成当前数据库对应的语句

咱们继续往下分析executeInsert的执行流程。

private long executeInsert(T entity, DatabaseStatement stmt, boolean setKeyAndAttach) {
    long rowId;
    if (db.isDbLockedByCurrentThread()) {
        rowId = insertInsideTx(entity, stmt);
    } else {
        db.beginTransaction();
        try {
            rowId = insertInsideTx(entity, stmt);
            db.setTransactionSuccessful();
        } finally {
            db.endTransaction();
        }
    }
    if (setKeyAndAttach) {
        updateKeyAfterInsertAndAttach(entity, rowId, true);
    }
    return rowId;
}
复制代码

这里首先是判断数据库是否被当前线程锁定,若是是,则直接插入数据,不然为了不死锁,则开启一个数据库事务,再进行插入数据的操做。最后若是设置了主键,则在插入数据以后更新主键的值并将对应的实体缓存到相应的identityScope中,这一块的代码流程以下所示:

protected void updateKeyAfterInsertAndAttach(T entity, long rowId, boolean lock) {
    if (rowId != -1) {
        K key = updateKeyAfterInsert(entity, rowId);
        attachEntity(key, entity, lock);
    } else {
       ...
    }
}

protected final void attachEntity(K key, T entity, boolean lock) {
    attachEntity(entity);
    if (identityScope != null && key != null) {
        if (lock) {
            identityScope.put(key, entity);
        } else {
            identityScope.putNoLock(key, entity);
        }
    }
}
复制代码

接着,咱们仍是继续追踪主线流程,在executeInsert这个方法中调用了insertInsideTx进行数据的插入。

private long insertInsideTx(T entity, DatabaseStatement stmt) {
    synchronized (stmt) {
        if (isStandardSQLite) {
            SQLiteStatement rawStmt = (SQLiteStatement) stmt.getRawStatement();
            bindValues(rawStmt, entity);
            return rawStmt.executeInsert();
        } else {
            bindValues(stmt, entity);
            return stmt.executeInsert();
        }
    }
}
复制代码

为了防止并发,这里使用了悲观锁保证了数据的一致性,在AbstractDao这个类中,大量使用了这种锁保证了它的线程安全性。接着,若是当前是标准数据库,则直接获取stmt这个DatabaseStatement类对应的原始语句进行实体字段属性的绑定和最后的执行插入操做。若是是加密数据库,则直接使用当前的加密数据库所属的插入语句进行实体字段属性的绑定和执行最后的插入操做。其中bindValues这个方法对应的实现类就是咱们的HistoryDataDao类。

public class HistoryDataDao extends AbstractDao<HistoryData, Long> {

    ...

    @Override
    protected final void bindValues(DatabaseStatement stmt, HistoryData     entity) {
        stmt.clearBindings();

        Long id = entity.getId();
        if (id != null) {
            stmt.bindLong(1, id);
        }
        stmt.bindLong(2, entity.getDate());

        String data = entity.getData();
        if (data != null) {
            stmt.bindString(3, data);
        }
    }
    
    @Override
    protected final void bindValues(SQLiteStatement stmt, HistoryData     entity) {
        stmt.clearBindings();

        Long id = entity.getId();
        if (id != null) {
            stmt.bindLong(1, id);
        }
        stmt.bindLong(2, entity.getDate());

        String data = entity.getData();
        if (data != null) {
            stmt.bindString(3, data);
        }
    }

    ...
}
复制代码

能够看到,这里对HistoryData的全部字段使用对应的数据库语句进行了绑定操做。这里最后再说起一下,若是当前数据库是加密型时,则会使用最开始说起的DatabaseStatement的加密实现类EncryptedDatabaseStatement应用代理模式去使用sqlcipher这个加密型数据库的insert方法

五、查询源码分析

通过对插入源码的分析,我相信你们对GreenDao内部的机制已经有了一些本身的理解,因为删除和更新内部的流程比较简单,且与插入源码有殊途同归之妙,这里就再也不赘述了。最后咱们再分析下查询的源码,查询的流程调用链较长,因此将它的核心流程源码直接给出。

List<HistoryData> historyDataList = historyDataDao.loadAll();

public List<T> loadAll() {
    Cursor cursor = db.rawQuery(statements.getSelectAll(), null);
    return loadAllAndCloseCursor(cursor);
}

protected List<T> loadAllAndCloseCursor(Cursor cursor) {
    try {
        return loadAllFromCursor(cursor);
    } finally {
        cursor.close();
    }
}

protected List<T> loadAllFromCursor(Cursor cursor) {
    int count = cursor.getCount();
    ...
    boolean useFastCursor = false;
    if (cursor instanceof CrossProcessCursor) {
        window = ((CrossProcessCursor) cursor).getWindow();
        if (window != null) {  
            if (window.getNumRows() == count) {
                cursor = new FastCursor(window);
                useFastCursor = true;
            } else {
              ...
            }
        }
    }

    if (cursor.moveToFirst()) {
        ...
        try {
            if (!useFastCursor && window != null && identityScope != null) {
                loadAllUnlockOnWindowBounds(cursor, window, list);
            } else {
                do {
                    list.add(loadCurrent(cursor, 0, false));
                } while (cursor.moveToNext());
            }
        } finally {
            ...
        }
    }
    return list;
}
复制代码

最终,loadAll方法将会调用到loadAllFromCursor这个方法,首先,若是当前的游标cursor是跨进程的cursor,而且cursor的行数没有误差的话,则使用一个加快版的FastCursor对象进行游标遍历。接着,不论是执行loadAllUnlockOnWindowBounds这个方法仍是直接加载当前的数据列表list.add(loadCurrent(cursor, 0, false)),最后都会调用到这行list.add(loadCurrent(cursor, 0, false))代码,很明显,loadCurrent方法就是加载数据的方法。

final protected T loadCurrent(Cursor cursor, int offset, boolean lock) {
    if (identityScopeLong != null) {
        ...
        T entity = lock ? identityScopeLong.get2(key) : identityScopeLong.get2NoLock(key);
        if (entity != null) {
            return entity;
        } else {
            entity = readEntity(cursor, offset);
            attachEntity(entity);
            if (lock) {
                identityScopeLong.put2(key, entity);
            } else {
                identityScopeLong.put2NoLock(key, entity);
            }
            return entity;
        }
    } else if (identityScope != null) {
        ...
        T entity = lock ? identityScope.get(key) : identityScope.getNoLock(key);
        if (entity != null) {
            return entity;
        } else {
            entity = readEntity(cursor, offset);
            attachEntity(key, entity, lock);
            return entity;
        }
    } else {
        ...
        T entity = readEntity(cursor, offset);
        attachEntity(entity);
        return entity;
    }
}
复制代码

咱们来理解下loadCurrent这个方法内部的执行策略。首先,若是有实体数据缓存identityScopeLong/identityScope,则先从缓存中取,若是缓存中没有,会使用该实体对应的Dao对象,这里的是HistoryDataDao,它在内部根据游标取出的数据新建了一个新的HistoryData实体对象返回。

@Override
public HistoryData readEntity(Cursor cursor, int offset) {
    HistoryData entity = new HistoryData( //
        cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0), // id
        cursor.getLong(offset + 1), // date
        cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2) // data
    );
    return entity;
}
复制代码

最后,若是是非identityScopeLong缓存类型,便是属于identityScope的状况下,则还会在identityScope中将上面得到的数据进行缓存。若是没有实体数据缓存的话,则直接调用readEntity组装数据返回便可。

注意:对于GreenDao缓存的特性,可能会出现没有拿到最新数据的bug,所以,若是遇到这种状况,可使用DaoSession的clear方法删除缓存。

3、GreenDao是如何与ReactiveX结合?

首先,看下与rx结合的使用流程:

RxDao<HistoryData, Long> xxDao = daoSession.getHistoryDataDao().rx();
xxDao.insert(historyData)
        .observerOn(AndroidSchedulers.mainThread())
        .subscribe(new Action1<HistoryData>() {
            @Override
            public void call(HistoryData entity) {
                // insert success
            }
        });
复制代码

在AbstractDao对象的.rx()方法中,建立了一个默认执行在io线程的rxDao对象。

@Experimental
public RxDao<T, K> rx() {
    if (rxDao == null) {
        rxDao = new RxDao<>(this, Schedulers.io());
    }
    return rxDao;
}
复制代码

接着分析rxDao的insert方法。

@Experimental
public Observable<T> insert(final T entity) {
    return wrap(new Callable<T>() {
        @Override
        public T call() throws Exception {
            dao.insert(entity);
            return entity;
        }
    });
}
复制代码

起实质做用的就是这个wrap方法了,在这个方法里面主要是调用了RxUtils.fromCallable(callable)这个方法。

@Internal
class RxBase {

    ...

    protected <R> Observable<R> wrap(Callable<R> callable) {
        return wrap(RxUtils.fromCallable(callable));
    }

    protected <R> Observable<R> wrap(Observable<R> observable) {
        if (scheduler != null) {
            return observable.subscribeOn(scheduler);
        } else {
            return observable;
        }
    }

    ...
}
复制代码

在RxUtils的fromCallable这个方法内部,其实就是使用defer这个延迟操做符来进行被观察者事件的发送,主要目的就是为了确保Observable被订阅后才执行。最后,若是调度器scheduler存在的话,将经过外部的wrap方法将执行环境调度到io线程。

@Internal
class RxUtils {

    @Internal
    static <T> Observable<T> fromCallable(final Callable<T> callable) {
        return Observable.defer(new Func0<Observable<T>>() {

            @Override
            public Observable<T> call() {
                T result;
                try {
                    result = callable.call();
                } catch (Exception e) {
                    return Observable.error(e);
                }
                return Observable.just(result);
            }
        });
    }
}
复制代码

4、总结

在分析完GreenDao的核心源码以后,我发现,GreenDao做为最好的数据库框架之一,是有必定道理的。首先,它经过使用自身的插件配套相应的freemarker模板生成所需的静态代码,避免了反射等消耗性能的操做。其次,它内部提供了实体数据的映射缓存机制,可以进一步加快查询速度。对于不一样数据库对应的SQL语句,也使用了不一样的DataBaseStatement实现类结合代理模式进行了封装,屏蔽了数据库操做等繁琐的细节。最后,它使用了sqlcipher提供了加密数据库的功能,在必定程度确保了安全性,同时,结合RxJava,咱们便能更简洁地实现异步的数据库操做。GreenDao源码分析到这里就真的完结了,下一篇,笔者将会对RxJava的核心源码进行细致地讲解,以此能让你们对RxJava有一个更为深刻的理解。

参考连接:

一、GreenDao V3.2.2源码

二、GreenDao源码分析

三、GreenDao源码分析

赞扬

若是这个库对您有很大帮助,您愿意支持这个项目的进一步开发和这个项目的持续维护。你能够扫描下面的二维码,让我喝一杯咖啡或啤酒。很是感谢您的捐赠。谢谢!


Contanct Me

● 微信:

欢迎关注个人微信:bcce5360

● 微信群:

微信群若是不能扫码加入,麻烦你们想进微信群的朋友们,加我微信拉你进群。

● QQ群:

2千人QQ群,Awesome-Android学习交流群,QQ群号:959936182, 欢迎你们加入~

About me

很感谢您阅读这篇文章,但愿您能将它分享给您的朋友或技术群,这对我意义重大。

但愿咱们能成为朋友,在 Github掘金上一块儿分享知识。

相关文章
相关标签/搜索