1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package cn.flightfeather.thirdappmodule.common.database
 
import cn.flightfeather.thirdappmodule.CommonApplication
import com.ping.greendao.gen.DaoSession
import io.reactivex.Observable
 
/**
 * @author riku
 * Date: 2019/5/16
 */
class DbFactory private constructor(private val application: CommonApplication){
 
    private var daoSession: DaoSession? = null
 
    init {
        daoSession = application.daoSession
    }
 
    companion object {
 
        @Volatile
        private var mDbFactory: DbFactory? = null
 
        //数据库初始化
        @JvmStatic
        @Synchronized
        fun init(application: CommonApplication) {
            if (mDbFactory == null) {
                mDbFactory = DbFactory(application)
            }
        }
 
        //获取数据库操作对象daoSession
        fun getGreenDaoObservable(): Observable<DaoSession> {
            return Observable.create { emitter ->
                mDbFactory?.daoSession?.let {
                    emitter.onNext(it)
                    emitter.onComplete()
                }
            }
        }
 
        fun getInstance(): DaoSession {
            if (mDbFactory == null) {
                mDbFactory = DbFactory(CommonApplication.getInstance())
            }
            return mDbFactory!!.daoSession!!
        }
    }
}