Skip to content

Commit

Permalink
优化 GreenDao 初始化位置,单独封装成类
Browse files Browse the repository at this point in the history
  • Loading branch information
HanteIsHante committed May 3, 2017
1 parent ea992a2 commit 09a1067
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 33 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.example.hante.thirdopen.application;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.example.hante.greendao.DaoMaster;
import com.example.hante.greendao.DaoSession;

/**
* GreenDao 管理类
*/

public class GreenDaoManager {

private static GreenDaoManager INSTANCE;
private static DaoSession mDaoSession;

public static GreenDaoManager getINSTANCE(){
if(INSTANCE == null) {
synchronized(GreenDaoManager.class){
if(INSTANCE == null) {
INSTANCE = new GreenDaoManager();
}
}
}
return INSTANCE;
}

static void init(Context context){
String DB_NAME = "thirdOpen-db";
// 通过DaoMaster 的内部类 DevOpenHelper,你可以得到一个便利的SQLiteOpenHelper 对象。
// 可能你已经注意到了,你并不需要去编写「CREATE TABLE」这样的 SQL 语句,因为greenDAO 已经帮你做了。
// 注意:默认的DaoMaster.DevOpenHelper 会在数据库升级时,删除所有的表,意味着这将导致数据的丢失。
// 所以,在正式的项目中,你还应该做一层封装,来实现数据库的安全升级。
DaoMaster.DevOpenHelper mDevOpenHelper = new DaoMaster.DevOpenHelper(context, DB_NAME,
null);
SQLiteDatabase mDatabase = mDevOpenHelper.getWritableDatabase();
DaoMaster mDaoMaster = new DaoMaster(mDatabase);
mDaoSession = mDaoMaster.newSession();
}
public DaoSession getDaoSession() {
return mDaoSession;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.example.hante.greendao.DaoMaster;
import com.example.hante.greendao.DaoSession;
import com.example.hante.thirdopen.util.LogUtils;
import com.squareup.leakcanary.LeakCanary;

Expand All @@ -27,10 +24,6 @@ public class MyApplication extends Application {
private static MyApplication instance;
private static Context mContext;

private SQLiteDatabase mDatabase;
private DaoSession mDaoSession;


@Override
public void onCreate () {
super.onCreate();
Expand All @@ -41,29 +34,8 @@ public void onCreate () {
instance = this;
mContext = getApplicationContext();
new LogUtils.Builder();
setDataBase();
}

private void setDataBase () {
// 通过DaoMaster 的内部类 DevOpenHelper,你可以得到一个便利的SQLiteOpenHelper 对象。
// 可能你已经注意到了,你并不需要去编写「CREATE TABLE」这样的 SQL 语句,因为greenDAO 已经帮你做了。
// 注意:默认的DaoMaster.DevOpenHelper 会在数据库升级时,删除所有的表,意味着这将导致数据的丢失。
// 所以,在正式的项目中,你还应该做一层封装,来实现数据库的安全升级。
DaoMaster.DevOpenHelper mDevOpenHelper = new DaoMaster.DevOpenHelper(this, "thirdOpen-db", null);
mDatabase = mDevOpenHelper.getWritableDatabase();
DaoMaster mDaoMaster = new DaoMaster(mDatabase);
mDaoSession = mDaoMaster.newSession();
}

public DaoSession getDaoSession() {
return mDaoSession;
GreenDaoManager.init(this); // 初始化 greenao 管理类
}


public SQLiteDatabase getDb() {
return mDatabase;
}

/**
* 获取Application 实例
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import com.example.hante.greendao.UserDao;
import com.example.hante.thirdopen.R;
import com.example.hante.thirdopen.ThirdOpenHomeActivity;
import com.example.hante.thirdopen.application.MyApplication;
import com.example.hante.thirdopen.application.GreenDaoManager;
import com.example.hante.thirdopen.db.bean.User;
import com.example.hante.thirdopen.util.Utils;

Expand All @@ -39,7 +39,7 @@ protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
ButterKnife.bind(this);
mUserDao = MyApplication.getInstance().getDaoSession().getUserDao();
mUserDao = GreenDaoManager.getINSTANCE().getDaoSession().getUserDao();
}

@OnClick({R.id.button_login, R.id.to_register})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

import com.example.hante.greendao.UserDao;
import com.example.hante.thirdopen.R;
import com.example.hante.thirdopen.application.MyApplication;
import com.example.hante.thirdopen.application.GreenDaoManager;
import com.example.hante.thirdopen.db.bean.User;
import com.example.hante.thirdopen.util.Utils;

Expand Down Expand Up @@ -39,7 +39,7 @@ protected void onCreate (Bundle savedInstanceState) {
setContentView(R.layout.activity_register);
ButterKnife.bind(this);
initUI();
mUserDao = MyApplication.getInstance().getDaoSession().getUserDao();
mUserDao = GreenDaoManager.getINSTANCE().getDaoSession().getUserDao();
}

private void initUI () {
Expand Down

0 comments on commit 09a1067

Please sign in to comment.