开发者

android db4o deactivate object properly

开发者 https://www.devze.com 2023-02-04 20:01 出处:网络
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 fo

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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消