Is it correct way to open and close GreenDao 3 connection?

411 Views Asked by At

Created Singleton-class:

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();
    }
}

In MyApplication class have been initialized it:

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

Specified MyApplication in the manifest:

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

In MainActivity class onCreate method I got list and put it to the 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);
    }

...

In onDestroy cleared the session and closed database:

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

In other activity setted the listener on the button:

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();
    }
});

All I want to know is:

  1. taskDao.detachAll(); - is it necessary?

  2. DataManager.getInstance().getDaoSession().clear(); - is it necessary to clear the session?

  3. DataManager.getInstance().getDatabase().close(); - and to close database?

Is it correct to call them before super.onDestroy();?

P.S. In the manifest file I setted only portrait orientation for all activities, so activities will not be re-created on screen rotation. Maybe I do it all in vain with Singleton?

0

There are 0 best solutions below