打开和关闭GreenDao3连接的方法正确吗?

uqzxnwby  于 2021-07-13  发布在  Java
关注(0)|答案(0)|浏览(273)

已创建单例类:

public class DataManager
{
    private static DataManager mDataManager;
    private static Database mDatabase;
    private static DaoSession mDaoSession;
    private static List<Task> mTasks;

    public static void initInstance(Context context)
    {
        if (mDataManager == null)
        {
            mDataManager = new DataManager();

            DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(context, "TASKS");
            mDatabase = helper.getWritableDb();
            mDaoSession = new DaoMaster(mDatabase).newSession();
            mTasks = mDaoSession.queryBuilder(Task.class).build().list();
        }
    }

    public static DataManager getInstance()
    {
        return mDataManager;
    }

    public Database getDatabase()
    {
        return mDatabase;
    }

    public DaoSession getDaoSession()
    {
        return mDaoSession;
    }

    public List<Task> getTaskList()
    {
        return mTasks;
    }

    public void updateTaskList()
    {
        mTasks = mDaoSession.queryBuilder(Task.class).build().list();
    }
}

在myapplication类中,已将其初始化:

public class MyApplication extends Application
{
    @Override
    public void onCreate()
    {
        super.onCreate();
        DataManager.initInstance(getApplicationContext());
    }
}

清单中指定的myapplication:

<application
        android:name=".MyApplication"
...

在mainactivity类oncreate方法中,我得到了list并将其放到 RecyclerView :

public class MainActivity extends AppCompatActivity
{
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        List<Task> tasks = DataManager.getInstance().getTasksList();

        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.rvMainRecycler);

        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);

        MainRecyclerViewAdapter mainRecyclerViewAdapter = new MainRecyclerViewAdapter(tasks);
        recyclerView.setAdapter(mainRecyclerViewAdapter);
    }

...

在ondestroy中,清除了会话并关闭了数据库:

@Override
protected void onDestroy()
{
    DataManager.getInstance().getDaoSession().clear();
    DataManager.getInstance().getDatabase().close();
    super.onDestroy();
}

在其他活动中,将侦听器设置在按钮上:

btnInsert.setOnClickListener(new View.OnClickListener()
{
    @Override
    public void onClick(View view)
    {
        TaskDao taskDao = DataManager.getInstance().getDaoSession().getTaskDao();

        Task task = new Task(/*здесь некоторые параметры*/);
        taskDao.insertOrReplaceInTx(task);

        taskDao.detachAll();
    }
});

我只想知道: taskDao.detachAll(); -有必要吗? DataManager.getInstance().getDaoSession().clear(); -是否需要清除会话? DataManager.getInstance().getDatabase().close(); -关闭数据库?
以前给他们打电话对吗 super.onDestroy(); ?
p、 在manifest文件中,我只为所有活动设置了纵向方向,因此活动不会在屏幕旋转时重新创建。也许我对辛格尔顿做的一切都是徒劳的?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题