简体   繁体   English

android db4o正确停用对象

[英]android db4o deactivate object properly

the way i'm using db4o is really just read-only. 我使用db4o的方式实际上只是只读的。 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. 但是对于我从数据库显示数据的活动,我不需要进行任何更新,因此出于这个原因,我只想从数据库中获取数据并“停用”该数据,以便在活动发生时由于屏幕方向改变或其他原因而导致的configurationChange,我可以快速重新布置活动。 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. 当您在android上更改屏幕方向时,它会关闭我的活动关闭方法,从而关闭数据库。 but then starts back up and I don't want to populate the mList again. 但随后开始备份,我不想再次填充mList。

so i keep getting a " com.db4o.ext.DatabaseClosedException " exception because in my layout of the activity i do things like 所以我不断收到“ com.db4o.ext.DatabaseClosedException ”异常,因为在我的活动布局中,我做类似

mList.size()

I really just want to deactivate the objects b/ci never have to update them, how can i do this? 我真的只是想停用b / ci的对象,而不必更新它们,我该怎么做?

Well first, you want ensure that all objects are loaded. 首先,您要确保所有对象都已加载。 db4o only returns a lazy loading list, which loads object when you access them. db4o仅返回一个惰性加载列表,该列表在您访问对象时会加载对象。 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. 现在您不应该获得DatabaseClosedException。

Then I just want to add that the 'deactivation' in db4o is something completely different. 然后我只想补充一下,db4o中的“停用”是完全不同的东西。 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. 显式停用仅在特殊情况下对安全存储有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM