android db4o 正确停用对象

发布于 2024-10-11 22:19:07 字数 686 浏览 11 评论 0原文

我使用 db4o 的方式实际上只是只读的。我将用应用程序打包一个数据库,并时不时地对其进行一些后台更新,我可以以不同的方式处理。但是对于显示来自数据库的数据的活动,我不需要进行任何更新,因此出于这个原因,我只想从数据库中获取数据并让该数据“停用”,以便当活动由于屏幕方向改变或其他原因导致的配置更改,我可以快速再次布局活动。停用对象以便它们仍然可用于我的活动的正确方法是什么。目前我有一些代码,就像

List<MyObject> mList = db().queryByExample(persistentClass);  // db just gets my ObjectContainer

我尝试做这样的事情

db().ext().deactivate(mList);

,但它似乎不起作用。我是否需要迭代列表中的每个项目来停用它?

当您更改 Android 上的屏幕方向时,它会触发我关闭数据库的活动关闭方法。但随后又重新启动,我不想再次填充 mList。

所以我不断收到“com.db4o.ext.DatabaseClosedException”异常,因为在我的活动布局中,我所做的事情就像

mList.size()

我真的只想停用对象 b/ci 永远不必更新它们,我该怎么做?

the way i'm using db4o is really just read-only. I will package a db with the application and do some background updating to it every now and again which i can handle differently. But for my activities that are displaying data from the db, i don't need to do any updates, so for this reason i would just like to get data out of the db and have that data be "deactivated" so when the activity has a configurationChange due to the screen orientation changing or something, i can quickly layout the activity again. What is the proper way to deactivate objects so they are still usable to my activity . Currently i have code something like

List<MyObject> mList = db().queryByExample(persistentClass);  // db just gets my ObjectContainer

I have tried doing something like this

db().ext().deactivate(mList);

but it has not seemed to work.Do i need to iterate through each item of the list to deactivate it?

when you change the screen orientation on the android, it hits my activity close method where i close the database. but then starts back up and I don't want to populate the mList again.

so i keep getting a "com.db4o.ext.DatabaseClosedException" exception because in my layout of the activity i do things like

mList.size()

I really just want to deactivate the objects b/c i never have to update them, how can i do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅黛梨妆こ 2024-10-18 22:19:07

首先,您要确保所有对象都已加载。 db4o 仅返回一个延迟加载列表,该列表会在您访问对象时加载它们。您需要将对象复制到不需要运行数据库的常规列表。这非常简单,只需将查询结果传递到一个新的数组列表:

 List<MyObject> mList = new ArrayList<MyObject>(db().queryByExample(persistentClass)); 

现在您不应该得到 DatabaseClosedException。

然后我只想补充一点,db4o 中的“停用”是完全不同的东西。它与 激活-机制。停用与激活相反。激活将对象从数据库加载到内存中。停用会使内存中的对象变成一个空壳,其中没有数据。显式停用仅对特殊情况有用,以确保内存安全。

Well first, you want ensure that all objects are loaded. db4o only returns a lazy loading list, which loads object when you access them. You need to copy your objects to a regular list which doesn't require a running database. This is quite simple, just pass the result of the query to a new array list:

 List<MyObject> mList = new ArrayList<MyObject>(db().queryByExample(persistentClass)); 

Now you shouldn't get a DatabaseClosedException.

Then I just want to add that the 'deactivation' in db4o is something completely different. It has to do with the Activation-mechanism. Deactivation is the opposite of activation. Activation loads to object from the database into memory. Deactivation makes the object in memory to an empty hull with no data in it. Explicit deactivation is only useful for special scenarios to safe memory.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文