Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
ingvar_e
Posts: 551
Registered: ‎10-31-2009
My Device: Torch 9800, Bold 9700
My Carrier: Movistar, Telenor
Accepted Solution

Save a class in persistent storage?

I have a class that have many configuration variables defined. These are set at runtime. Is it possible to save and retrieve the class in persistent storage? If yes, how?

If everything seems to be under control, you're just not driving fast enough
-Mario Andretti-
Please use plain text.
Developer
simon_hain
Posts: 15,806
Registered: ‎07-29-2008
My Device: Z10 LE
My Carrier: O2 Germany

Re: Save a class in persistent storage?

sure, just implement persistable for the class (and all subclasses).
if you use api classes that are not persistable you have to replace them, for example long instead of date.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Please use plain text.
Developer
peter_strange
Posts: 19,595
Registered: ‎07-14-2008
My Device: Not Specified

Re: Save a class in persistent storage?

To find related KB articles, including sample code, type "persistence" in the Search box.  Here is one that I found. 

 

http://supportforums.blackberry.com/t5/Java-Development/Introduction-to-Persistence-Models-on-BlackB...

Please use plain text.
Developer
ingvar_e
Posts: 551
Registered: ‎10-31-2009
My Device: Torch 9800, Bold 9700
My Carrier: Movistar, Telenor

Re: Save a class in persistent storage?

I am using persistent storage for several purposes.

 

For some specific reasons I would like to store some config variables in a class and make it accessible from

2 different major tasks running. A background task and a GUI. If possible I would like this class to keep its values between restarts

 

I have made a few attempts with a test class like this:

 

 

package combo;
import net.rim.device.api.util.Persistable;

public class StatConfigs implements Persistable
 {
   public int tester = 0;
 }

 

Can not get a grip on it from my 2 tasks running.

 

Have searched this site for persistable class but not found much. Many articles on how to handle persistent storage yes, but I have no broblem with that.

 

Is it possible to access "tester" from two different (main) threads?  Is the value of "tester" preserved

"automatically"

 

If everything seems to be under control, you're just not driving fast enough
-Mario Andretti-
Please use plain text.
Developer
peter_strange
Posts: 19,595
Registered: ‎07-14-2008
My Device: Not Specified

Re: Save a class in persistent storage?

The situation you are trying to code for is easily handled using persistent store. 

 

If you look at the way persistent store works, you basically store an Object in a persistent slot, which is identified by an ID - the ID is along value.  So anyone who knows the ID can access the Object. 

 

However to interpret the Object, they must have the exact class that was used when the Object was stored.  In your case this is easy as your two 'Applications' will actually use the same class as they are actually just different entry points into the same set of code. 

 

This requirement for the same class to be used, becomes a problem when you have two completely different applications using the same Persistent ID.  In this situation, the two applications have to share a Library class that defines the persistent Object.

 

But back to your situation, what you are trying to do will work well with Persistent Store.

 

To give you more of feel for it, when your background application requests the persistent object, it will get a reference to an Object in flash memory.  When your GUI application does the same thing, it will get a reference to the same Object.  So they will definitely share the data and both see each other's updates.

 

Hope that clears this up for you. 

Please use plain text.
Developer
ingvar_e
Posts: 551
Registered: ‎10-31-2009
My Device: Torch 9800, Bold 9700
My Carrier: Movistar, Telenor

Re: Save a class in persistent storage?

Ah!

 

The key to the problem is to use a Library in conjunction with a persistable class then. I already have the correct code in place to access a persistent storage in a controlled way from my  two "apps"

If everything seems to be under control, you're just not driving fast enough
-Mario Andretti-
Please use plain text.
Developer
peter_strange
Posts: 19,595
Registered: ‎07-14-2008
My Device: Not Specified

Re: Save a class in persistent storage?

Typically when I have a background task and a GUI, they are actually part of the same application and installed together.  If you can do this, package them together and only have a single project, lie will be much similar.  If you start looking at getting Libraries to access shared data, the packaging becomes significantly more complicated.  Can you build these two together.

Please use plain text.
Developer
ingvar_e
Posts: 551
Registered: ‎10-31-2009
My Device: Torch 9800, Bold 9700
My Carrier: Movistar, Telenor

Re: Save a class in persistent storage?

This is actually an upgrade of an existing application so the over all architecture is rather fixed. In an earlier relaease we actually had a libray but dropped it. I believe the solution actually will be done in 2 parts. Shared configs will be stored directly in persistent storage (As in the current production version). Another person is developing the GUI that has conf variables of a different type and since the GUI is the only user of these configs it will be implemented with a class. No need to share so the problem is simpler. Split the problem into 2 parts  :-)

If everything seems to be under control, you're just not driving fast enough
-Mario Andretti-
Please use plain text.