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
Posts: 170
Registered: ‎01-17-2009
My Device: 9630
Accepted Solution

Persistent Storage Problems...

[ Edited ]

Hi,

 

I was reluctant to post this because I thought it was a simple solution so I tried working around it. Basically what happens is it saves, I can close my application then reopen and it works. That's until I do a battery pull and it all disappears. Is this a problem with my source?

 

// updated source code 

 

public static ProjectVector get() {

active = true;

ProjectVector vector = new ProjectVector();

PersistentObject persist = PersistentStore.getPersistentObject(key);

Object[] object = (Object[]) persist.getContents();

if(object != null)

vector = decode(object);

active = false;

return vector;

}

 

public static boolean save() {

if(active)

return false;

PersistentObject persist = PersistentStore.getPersistentObject(key);

Object[] object = encode(HomeScreen.projects);

persist.setContents(object);

persist.forceCommit();

return true;

}

 

private static Object[] encode(Vector vector) {

Object[] object = new Object[vector.size()];

for(int i = 0; i < object.length; i++)

object[i] = ((Project)vector.elementAt(i)).toObject();

return object;

}

 

private static ProjectVector decode(Object[] object) {

ProjectVector vector = new ProjectVector();

for(int i = 0; i < object.length; i++)

vector.addElement(new Project((Object[])object[i]),true);

return vector;

}

 

 

I had a Vector that contained another Vector with String objects inside it, but that failed also. I then moved to multi dimensional Object's but that's failing. Does anybody have advice? Should I try Strings and split them? 

Message Edited by gregzav on 02-12-2009 01:53 AM
Message Edited by gregzav on 02-12-2009 04:21 PM
Message Edited by gregzav on 02-12-2009 04:21 PM
Developer
Posts: 3,951
Registered: ‎07-15-2008
My Device: Not Specified

Re: Persistent Storage Problems...

Just to confirm, where are you using method "commit()".

Use Search. "Accept Solution" If the problem is resolved.
Developer
Posts: 170
Registered: ‎01-17-2009
My Device: 9630

Re: Persistent Storage Problems...

I had commit somewhere elsewhere in the application and that wasn't the problem. I added it into the save method though for the sake of cleanliness. Unfortunately I still can't figure out my problem. I assume that BlackBerry's are like Linux in the way that they don't save information until they have to, so I tried the forceCommit method. This loses all of the information including what I had saved before. I also tested the Persistent Storage Demo application and it also loses data on a battery pull. Has anybody found a solution?
Developer
Posts: 19,631
Registered: ‎07-14-2008
My Device: Not Specified

Re: Persistent Storage Problems...

I have had experience where uncommited Persistent objects have appeared updated to all viewing applications, but the updates are lost on a battery pull.  So I agree with BBDev's comment that it was missing in the first variation of the code you posted.

 

I take it you have proved that a encode, followed by a decode doesn't cause a problem, in other words, that the data is not being lost in that process.  I have to say it seems a bit odd the way you are converting a Vector into an Object Array, why are you not just storing the ProjectVector?

 

Anyway, I'd do the following:

 

Split the lines

 

Object[] object = (Object[]) persist.getContents();

if(object != null)

 

into the following:

 

Object o = persist.getContents();

if ( o != null && o instanceof (Object []) ) {

Object[] object = (Object[]) o;

 

Now add a break point at

Object o = persist.getContents();

At this point check the key is correct, then let the getContents() proceed and check the object returned, and watch the processing as the object is cast.

 

Finally at a break point at:

persist.setContents(object);

At this point check the key is correct, and that object is what you expect it to be.  With these two break points in place, go through battery pull (i.e. restart the simulator) and make sure the object you get back on the get() is what you expect.  This will determine whether the problem is in the persistence or the encode/decode logic.

 

Hope this helps.

Developer
Posts: 170
Registered: ‎01-17-2009
My Device: 9630

Re: Persistent Storage Problems...

[ Edited ]
Peter if I wanted to store my ProjectVector would I have to cast it as a Vector? It extends the Vector class, and I implemented Persistable, but I am thrown a NonPersistableObjectException when I try to save the contents. Forgot to mention all of the referenced classes have implemented Persistable. I only have a reference to a "Date" in one of the classes which I'm not sure about but that can't be it.
Message Edited by gregzav on 02-13-2009 06:11 PM
BlackBerry Development Advisor
Posts: 15,140
Registered: ‎07-09-2008
My Device: BlackBerry Passport
My Carrier: Bell

Re: Persistent Storage Problems...

A NonPersistableObjectException indicates that you are trying to persist an object that doesn't implement Persistable.  I recommend verifying that all of the objects within your Vector do implement the Persistable interface.

Mark Sohm
BlackBerry Development Advisor

Please refrain from posting new questions in solved threads.
Problem solved? Click the Accept As Solution button.
Found a bug? Report it using Issue Tracker
Developer
Posts: 170
Registered: ‎01-17-2009
My Device: 9630

Re: Persistent Storage Problems...

I have gone through my source and everything I'm storing implements Persistable, except one class has a Date object. Would that be enough to throw the exception?
Developer
Posts: 170
Registered: ‎01-17-2009
My Device: 9630

Re: Persistent Storage Problems...

Nevermind...:

TodoItem: Error!: Class TodoItem marked Persistable by interface: net.rim.device.api.util.Persistable is not Persistable: field dueDate: Class java.util.Date does not implement net.rim.vm.Persistable