Clear Data From Cache Using GreenDAO

試著忘記壹切 提交于 2019-12-11 05:57:08

问题


In my app, there are 4 Activities starting from activities 1 to 4.

Database creation takes place in activity1 and values are updated in activities 2, activities 3 etc.But when i moves back to activities using finish() from activities 3 to activities 2 ,it wont shown the updated value because of cache.(Cant able to show the updated value)

i tried daosession.clear() ,but it is not clearing the data.

So how to clear old cache and reload the data using GreenDAO.

In all these activities , i used separate session .See sample code

Suppose current activity is CaptureLocationDetails class

public static CaptureLocationDetails getInstance()
{
    return instance;
}

   openHelper = new DevOpenHelper(MyApplication.getInstances());
    //openHelper.openDatabase();
    db = openHelper.getWritableDatabase();
    daoMaster = new DaoMaster(db);

    daoSession = daoMaster.newSession();



-----

----


 daoSession.getUserDao().update(entity);

then

GetMap class (Next Activity)

DevOpenHelper openHelper = new DevOpenHelper(MyApplication.getInstances());
    db = openHelper.getWritableDatabase();
    daoMaster = new DaoMaster(db);
    daoSession = daoMaster.newSession();

----

 upPackLinear.setOnClickListener(new OnClickListener() {

        @Override
          public void onClick(View v)
        {

            //DaoSession.this.clear();


            //daoSession.clear();
            finish();

            CaptureLocationDetails.getInstance().daoSession.clear();

        }
    });

But when i returned back to CaptureLocationDetails class , it loads value from cache.


回答1:


You can do it with 2 approaches.

1. startActivityForResult() approach

Follow the following steps. If confused, you may check this tutorial.

  1. start your 2nd Activity using startActivityForResult()
  2. Override the onActivityResult() in the 1st Activity. There, clear the DB cache and refetch the data.
  3. before finish() call in 2nd Activity, call setResult().

2. onResume() approach

  • clear the DB cache and load the data in onResume().

  • This way every time the Activity shows up, data will be fetched. It's not efficient because every time your Activity comes to foreground, onResume() is called.




回答2:


Simple Solution is

1:Just moves from one next activity to the previous activity using Intent(Not through finish() )

It clears old cache and loads the updated value..

Hope this helps somebody.. :)



来源:https://stackoverflow.com/questions/23890400/clear-data-from-cache-using-greendao

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!