Android GreenDao返回损坏的数据

时间:2016-10-11 12:15:49

标签: android greendao android-annotations

我使用GreenDao版本2创建了一个应用程序。我遇到了一个问题,它返回了我的实体而没有一些参数。

@EApplication
public class App extends MultiDexApplication {

    public static App instance = null;
    private static Context ctx;
    @Pref
    protected UserPrefs_ userPrefs;
    private DaoHelper daoHelper = new DaoHelper();

    public static UserPrefs_ getUserPrefs() {
        return instance.userPrefs;
    }

    public static Context getCtx() {
        return ctx;
    }

    public static App getInstance() {
        return instance;
    }

    public static DaoSession getNewSession() {
        return getInstance().daoHelper.getSession(true, ctx);
    }

    public static DaoSession getSession() {
        return getInstance().daoHelper.getSession(false, ctx);
    }


    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
        ctx = getApplicationContext();
    }
}

DaoHelper.class

public class DaoHelper {
    private static final String TAG = "GreenDao";
    private static final String DB_NAME = "db";
    private static Context context = null;
    private SQLiteDatabase db = null;
    private DaoSession session = null;

    private DaoMaster getMaster() {
        Log.v("greenDAO", "getMaster");
        if (db == null) {
            db = getDatabase(DB_NAME, false);
        }
        return new DaoMaster(db);
    }

    public DaoSession getSession(boolean newSession, Context context) {
        Log.v("greenDAO", "getSession");
        DaoHelper.context = context;
        if (newSession) {
            return getMaster().newSession();
        }
        if (session == null) {
            session = getMaster().newSession();
        }
        return session;
    }

    private synchronized SQLiteDatabase getDatabase(String name, boolean readOnly) {
        Log.v("greenDAO", "getDatabase");
        String s = "getDB(" + name + ",readonly=" + (readOnly ? "true" : "false") + ")";
        try {
            readOnly = false;
            Log.i(TAG, s);
            SQLiteOpenHelper helper = new MyOpenHelper(context, name, null);
            if (readOnly) {
                return helper.getReadableDatabase();
            } else {
                return helper.getWritableDatabase();
            }
        } catch (Exception ex) {
            Log.e(TAG, s, ex);
            return null;
        } catch (Error err) {
            Log.e(TAG, s, err);
            return null;
        }
    }

DaoService.class

public class DaoService {

    private static DaoSession daoSession = App_.getSession();

    public static User getMyProfile() {
        return daoSession.getUserDao()
                .queryBuilder()
                .where(UserDao.Properties.Type.eq(BuildConfig.FLAVOR))
                .list()
                .get(0);
    }
}

我打电话给DaoService.getMyProfile()时有2个案例:

a)当我通过活动启动应用程序时,在我调用该方法后,我会收到我的用户的所有数据(用户名,电话号码,电子邮件等)

b)当我从后台服务调用该方法时,我只使用几个参数接收我的用户,而不是所有参数,例如a)

是否有任何错误的实施或遗漏了什么?

0 个答案:

没有答案