使用GreenDao的工具类

在生成database后,我们要使用它,所以写一个工具类,就是单例模式,

返回一个DaoSession

public class DaoManager {
    private static DaoManager daoManager;
    private final DaoSession daoSession;

    private DaoManager(Context context) {
        daoSession = DaoMaster.newDevSession(context, "my.db");
    }

    public static DaoManager instance(Context context) {
        if (daoManager == null) {
            synchronized (DaoManager.class) {
                if (daoManager == null) {
                    daoManager = new DaoManager(context);
                }
            }
        }
        return daoManager;
    }

    public DaoSession getDaoSession() {
        return daoSession;
    }
}

你可能感兴趣的:(使用GreenDao的工具类)