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
Trusted Contributor
arunimi123
Posts: 182
Registered: ‎09-22-2011
My Device: 9700

Re: Persistent object not deleting when handheld restarts

@Maadani

         How can u create a class which can extend String,coz String is a Final class,and we cant violate the rules of java.

Developer
maadani
Posts: 729
Registered: ‎05-04-2011
My Device: 9700

Re: Persistent object not deleting when handheld restarts

You are right. Bad example...

 

Try extending a Hash Table and make it implement the "Persistable" interface.

 

Then you can add the Strings to your extended hash table.

 

E.

Trusted Contributor
arunimi123
Posts: 182
Registered: ‎09-22-2011
My Device: 9700

Re: Persistent object not deleting when handheld restarts

@Maadani sir,i did similar thing,but i get Classcastexception.I am putting my two claases here

 

Class that extends Hashtable and imnplements Persistable

 

 

package com.imimobile.common;

import java.util.Hashtable;

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


public class PersisteceStoreHandler extends Hashtable implements Persistable{
public PersisteceStoreHandler(){
super();
}

}

 

 

The actual class where i am using persistence

 

 

 

package com.imimobile.common;

import java.util.Hashtable;

import net.rim.device.api.crypto.SHA1Digest;
import net.rim.device.api.system.PersistentObject;
import net.rim.device.api.system.PersistentStore;


public class PersistentStorage {

public static PersistentObject pObject;
public Hashtable testHashTable1;
public PersistentStorage() {
pObject = PersistentStore.getPersistentObject(0xe609de484b304c61L);
}

public void setObject(String keyString, Object val) {
synchronized (pObject){
String keystr=keyString;

testHashTable1 = (PersisteceStoreHandler)pObject.getContents();
if (null == testHashTable1)
{
testHashTable1 = new Hashtable();
}
testHashTable1.put(keystr+"", val);
pObject.setContents(testHashTable1);
pObject.commit();
}
}

public Object getObject(String keyString) {
synchronized (pObject){
String keystr=keyString;
Object object;

testHashTable1 = (PersisteceStoreHandler)pObject.getContents();
if (null != testHashTable1 && testHashTable1.size() != 0)
{
return testHashTable1.get(keystr);
} else
{
return null;
}
}
}

}

Developer
maadani
Posts: 729
Registered: ‎05-04-2011
My Device: 9700

Re: Persistent object not deleting when handheld restarts

See below the class that I'm using.

 

import java.util.Hashtable;

import net.rim.device.api.util.*;
import net.rim.device.api.system.*;

//
// class UserStore
// 
// This class provides a persistent repository for data to be maintained across sessions.
// 

public class UserStore
{
    private static long KEY = 0x132456789012345L;     
    
    private PersistentObject   _persist;
    private PersistentHash _ph;
    
    private static UserStore _instance;
    
    public static UserStore getUserStore(){
    	if (_instance == null){
    		_instance = new UserStore();
    	}
    	return _instance;
    }
    private UserStore()
    {
        _persist = PersistentStore.getPersistentObject(KEY);
        
        try
        {
            _ph = (PersistentHash)_persist.getContents();
        }
        catch (Exception ex)
        {
            //This will catch a ClassCastException, which can occur when the
            //persistent store has been corrupted. 
        }
        
        if (_ph == null)
        {
            _ph = new PersistentHash();
            _persist.setContents(_ph);
            _persist.commit();
        }
    }
    
    public synchronized void delete()
    {
    	_ph.clear();
    	_persist.setContents(_ph);
    	_persist.commit();
    	PersistentStore.destroyPersistentObject(KEY);
    }
    
    public synchronized void setValue(String key, Object value)
    {       
        _ph.put(key.toLowerCase(), value);
        _persist.commit();
    }
    
    public synchronized Object getValue(String key)
    {               
        return _ph.get(key.toLowerCase());
    }
    
    private static class PersistentHash extends Hashtable implements Persistable
    {}
}

 It's not the most efficient but it does the work.

 

E.

New Contributor
nkuntala
Posts: 5
Registered: ‎10-03-2011
My Device: 9800

Re: Persisntent object not deleting when handheld restarts

Put the app in autostart mode and have your cleanup code i.e. destroyPersistent(...) in the main thread so that whenever the app restartes, your persistent object is cleared and you can create one as need be in your application.