清除数据从缓存中使用GreenDAO [英] Clear Data From Cache Using GreenDAO
问题描述
在我的应用程序中,有4 活动
从活动开始1到4。
In my app, there are 4 Activities
starting from activities 1 to 4.
数据库
创作发生在活动1和值在活动2更新,活动3 etc.But当我使用完成(移回到活动)
活动从3到活动2,它不会显示更新的值,因为缓存。(广东话能够显示更新后的值)
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)
我试过 daosession.clear()
,但它不清除数据。
i tried daosession.clear()
,but it is not clearing the data.
因此,如何清除旧的缓存并使用重新加载数据 GreenDAO
。
So how to clear old cache and reload the data using GreenDAO
.
在所有这些活动中,我使用单独的会话。看到样品code
In all these activities , i used separate session .See sample code
假设当前的活动是 CaptureLocationDetails
类
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);
然后
的GetMap
类(下一个活动)
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();
}
});
但是,当我返回到 CaptureLocationDetails
类,它从缓存中加载值。
But when i returned back to CaptureLocationDetails
class , it loads value from cache.
推荐答案
您还可以用两个方法做到这一点。
You can do it with 2 approaches.
请按照以下步骤。如有不明之处,您可以检查本教程。
Follow the following steps. If confused, you may check this tutorial.
- 启动第二届
活动
使用startActivityForResult()
-
覆盖
的的onActivityResult()
在第1活动
。在那里,清除DB缓存和重新获取数据。 - 在
完成()在第2
通话活动
,电话的setResult()
。
- start your 2nd
Activity
usingstartActivityForResult()
Override
theonActivityResult()
in the 1stActivity
. There, clear the DB cache and refetch the data.- before
finish()
call in 2ndActivity
, callsetResult()
.
-
清除数据库高速缓存和负载在
在onStart数据()
。
这样每次活动
显示时间,数据将被获取。这不是有效的,因为每一次你的活动
进来前景,在onStart()
被调用。
This way every time the Activity
shows up, data will be fetched. It's not efficient because every time your Activity
comes in foreground, onStart()
is called.
这篇关于清除数据从缓存中使用GreenDAO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!