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. New to the forum? Please visit the ‘Getting Started’ link below.
inside custom component

Java Development

Reply
New Developer
New Developer
tkf
Posts: 9
Registered: ‎03-17-2011
My Carrier: Megafon

Strange Persistance Behaviour

[ Edited ]

I'm building app that uses Persistable object to store data. Before i stored only simple data types, such as int/string and everyting was find. Some time ago i tried to store Hashtable that also implement persistable interfaces, and according to documentation must me stored without any problem

 

public class PhotoShareProperties  implements Persistable{
    private String access_token;
    private Hashtable albums2;
    private static final long PERSISTENCE_ID = 0x2abgf1c291f4412dL;
    private static PersistentObject store;
    static {
        store = PersistentStore.getPersistentObject(PERSISTENCE_ID);
        synchronized (store) {
            if (store.getContents() == null) {
                store.setContents(new PhotoShareProperties());
                store.commit();
            }
        }
    } 
     private PhotoShareProperties()
    {
        access_token="";
        albums2=null;
    }    

    public static PhotoShareProperties fetch()
    {
    	PhotoShareProperties savedProps =
          (PhotoShareProperties) store.getContents();
        return new PhotoShareProperties(savedProps);
    }
 public void save()
    {      
    	synchronized (store) {
        	store.setContents(this);
        	store.commit();
    	}
    }
    public void setAlbums(BigVector albumsLocal)
    {

    	albums2=null;
    	if (albums2==null)
    		albums2=new Hashtable();    	
    	for (int i=0; i<albumsLocal.size();i++)
    	{
    		Hashtable alb_tmp=new Hashtable();
    		Album a=(Album) albumsLocal.elementAt(i);
    		alb_tmp.put("name", a.name);
    		alb_tmp.put("uid", a.uid);
    		albums2.put(String.valueOf(i),alb_tmp);
    	}
    }
}


 There are also some getters setters and fields that i haven't included, but they don't make sense here.

 I am using separate thread to get data from network and store it in ths persistant object. 

 There is something like this:

PhotoShareProperties optionProperties = PhotoShareProperties.fetch();
optionProperties.setAccessToken(_access_token);
optionProperties.setAlbums(_album_list);
optionProperties.save();
optionProperties=VKPhotoShareProperties.fetch();

 Last line is used for testing. Problem is that before calling save (and also after it) object got all the fields as it should. But after fetching (even if i fetch after a decent amount of time, so that save should have finished normally) albums2 field becaming NULL. It looks a kind like magic, everything saved without warnings, other fields (that are basis data types such as string) are saved and fetched normally, only hashtable  disappear. 

 

p.s. i am using Blackberry SDK for OS 5, app is debugged on Blackberry 9700 smartphone with OS6 installed.

Please use plain text.
Developer
peter_strange
Posts: 17,664
Registered: ‎07-14-2008

Re: Strange Persistance Behaviour

I'm not sure I understand the second part of your question, but I hope I can help with the first.

 

If you reference a persistent object, in two different places, you will most likely in fact, reference the same Object.  So updates done by one part of the app can be seen immediately by another part of the app.

 

However these updates will be lost unless you commit them.  However committing does not impact on the contents, it just make sure that the Object is persisted.

 

I think that explains why you see updates to the Object before you save.  What it does not explain is why you loose the Hashtable reference when you do save().  I suspect there is a problem in your save() code and this is why it looses the reference.  So debug it carefully. 

Please use plain text.
New Developer
New Developer
tkf
Posts: 9
Registered: ‎03-17-2011
My Carrier: Megafon

Re: Strange Persistance Behaviour

Save() just commit changes, saving the current object. IMO there shouldn't be  any magic to save some fields. Because String fields are saved without anymagic, they implement persistable interface just as Hashtable does, so it should be stored automatically. But it just get lost. When debugging save everything seems to be normal, but then restoring i continue to get empty field

Please use plain text.
Developer
peter_strange
Posts: 17,664
Registered: ‎07-14-2008

Re: Strange Persistance Behaviour

Hashtables are persisted exactly like Strings.   If you persist an Object that has a reference to a persistable Hashtable, that reference and the associated Hastable is also persisted.  If in the proces of persisting and recovering the data, you loose the reference to the Hastable, then I suspect the problem is not in the persisting process, but is in some other processing that is going on. 

Please use plain text.
New Developer
New Developer
tkf
Posts: 9
Registered: ‎03-17-2011
My Carrier: Megafon

Re: Strange Persistance Behaviour

But why then string references are stored? All missed code is additional properties and their getters/setters. So it is strange that it lose some of the references, while persisting others.

Please use plain text.
Developer
peter_strange
Posts: 17,664
Registered: ‎07-14-2008

Re: Strange Persistance Behaviour

Since persisting Hashtables should work according to the documentation, and does work in my testing, I suspect there is an issue in your implementation.  

 

I suggest you try a simple standard implementation with just Hashtables and Strings and none of your extended classes, and see if the problem occurs. 

Please use plain text.