| | |
| | | * @author riku |
| | | * Date: 2019/5/16 |
| | | */ |
| | | class DbFactory private constructor(application: CommonApplication){ |
| | | class DbFactory private constructor(private val application: CommonApplication){ |
| | | |
| | | private var daoSession: DaoSession? = null |
| | | |
| | | init { |
| | | daoSession = application.daoSession |
| | | } |
| | | |
| | | companion object { |
| | | |
| | | @Volatile |
| | | private lateinit var daoSession: DaoSession |
| | | private var mDbFactory: DbFactory? = null |
| | | |
| | | //数据库初始化 |
| | | @JvmStatic |
| | | @Synchronized |
| | | fun init(application: CommonApplication) { |
| | | daoSession = application.daoSession |
| | | if (mDbFactory == null) { |
| | | mDbFactory = DbFactory(application) |
| | | } |
| | | } |
| | | |
| | | //获取数据库操作对象daoSession |
| | | fun getGreenDaoObservable(): Observable<DaoSession> { |
| | | return Observable.create { emitter -> |
| | | daoSession.let { |
| | | mDbFactory?.daoSession?.let { |
| | | emitter.onNext(it) |
| | | emitter.onComplete() |
| | | } |
| | |
| | | } |
| | | |
| | | fun getInstance(): DaoSession { |
| | | return daoSession |
| | | if (mDbFactory == null) { |
| | | mDbFactory = DbFactory(CommonApplication.getInstance()) |
| | | } |
| | | return mDbFactory!!.daoSession!! |
| | | } |
| | | } |
| | | } |