问题
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.
- start your 2nd
ActivityusingstartActivityForResult() OverridetheonActivityResult()in the 1stActivity. There, clear the DB cache and refetch the data.- before
finish()call in 2ndActivity, callsetResult().
2. onResume() approach
clear the DB cache and load the data in
onResume().This way every time the
Activityshows up, data will be fetched. It's not efficient because every time yourActivitycomes 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