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
New Developer
banepu
Posts: 14
Registered: ‎08-15-2010
My Device: Blackberry Curve 8330
Accepted Solution

Delete Persistent Data when the app is uninstalled

Hello all,

           I searched a lot on the Internet to find how to delete persistent data when an app is uninstalled and found quite a bit of information. Everywhere it says the persistent data will automatically be deleted when an app is uninstalled as long as it is implemented as a custom class and it is not shared by any other app. But unfortunately things are not working for me.

 

I'm using Eclipse SDK 3.5 with Blackberry plugin 1.1 and writing this app for Blackberry OS 4.5.0 and 5.0.0.

 

Below is the "persistent data" code I have. I got this from the Internet and it is working great in every aspect except that the data is not removed when I uninstall my app.

 

 

public final class persistentDataStore implements Persistable
{

/* derive TEST_DATA_ID from TestAppDataID1 */
private long TEST_DATA_ID = 0x4aa1df3f34676aa0L;
private static PersistentObject testDataBase1;
private LongHashtableCollection testHashTable1;

public long FIRST_ENTRY = 0;

public persistentDataStore()
{
testDataBase1 = PersistentStore.getPersistentObject(TEST_DATA_ID);
}

/* Method to retrieve a saved "string" value */
public String getStringValue(long key)
{
Object result = get(key);
return (null != result) ? (String) result : " ";
}

/* Method to save a "string" value */
public void setStringValue(long key, String value)
{
set(key, (Object)value);
}

/* Method to store an object in persistent data */
private void set(long key, Object value)
{
synchronized (testDataBase1)
{
testHashTable1 = (LongHashtableCollection)testDataBase1.getContents();
if (null == testHashTable1)
{
testHashTable1 = new LongHashtableCollection();
}
testHashTable1.put(key, value);
testDataBase1.setContents(testHashTable1);
testDataBase1.commit();
}
}

/* Method to retrieve an object from persistent data */
private Object get(long key)
{
synchronized (testDataBase1)
{
testHashTable1 = (LongHashtableCollection)testDataBase1.getContents();
if (null != testHashTable1 && testHashTable1.size() != 0)
{
return testHashTable1.get(key);
} else
{
return null;
}
}
}
}

 

 

In my main app, I have just one simple "BasicEditField" for the user to input a string and a "Save" button to save it. I can provide that code as well if someone wants it.

 

When I enter something in the text field and hit the save button, the string I enter is being saved. When I close and open the app, it does open with the last saved string as expected. But when I uninstall the app and re-install it, even the first time I open the app, it displays the last string saved before the previous uninstall. Everytime I uninstall and reinstall the app, I want the text field to be empty.

 

Please let me know what is missing here.

 

Thanks for your help in advance!


---------
This knowledge sharing is invaluable!
Please use plain text.
Developer
Ted_Hopp
Posts: 1,305
Registered: ‎01-21-2009
My Device: Not Specified

Re: Delete Persistent Data when the app is uninstalled

Your persistent object content is a LongHashtableCollection, which is not a custom class. Try declaring and using your own subclass of LongHashtableCollection (it doesn't have to override or add any functionality, but it does need to explicitly implement Persistable). Then it should disappear when the app is uninstalled. The only line of your existing code you need to change is

 

testHashTable1 = new MyLongHashtableCollectionSubclass();

(On startup, you might want to check whether there's already a PersistentObject under your key containing an instance of your subclass. If persistent data exists but the content is a plain old LongHashtableCollection, you need to replace the content before this will work.)




Solved? click "Accept as solution". Helpful? give kudos by clicking on the star.
Please use plain text.
New Developer
banepu
Posts: 14
Registered: ‎08-15-2010
My Device: Blackberry Curve 8330

Re: Delete Persistent Data when the app is uninstalled

Thanks for your response Ted_Hopp. It is working now.

 

I have a question though. I could not create a subclass for "LongHashtableCollection"as it said I cannot "create a subclass of the final class LongHashtableCollection". Hence I used "LongHashtable" and created a sublass for this one. This works for me but I wanted to know if I want to use LongHashtableCollection instead of LongHashtable, can I do it? Also, what is the exact difference between the two?


---------
This knowledge sharing is invaluable!
Please use plain text.
Developer
Ted_Hopp
Posts: 1,305
Registered: ‎01-21-2009
My Device: Not Specified

Re: Delete Persistent Data when the app is uninstalled

I'm sorry that I hadn't noticed that LongHashtableCollection is declared final. Go ahead with LongHashtable; that should work fine for what you are doing.

 

A LongHashtableCollection is, basically, a replacement for LongHashtable that implements Collection and related interfaces, making it usable with the net.rim.device.api.collection framework. If you want to use it, you can write your custom class to include a LongHashtableCollection as a member, and write your custom class to provide whatever access functions you need. (This is basically the Adapter design pattern.)




Solved? click "Accept as solution". Helpful? give kudos by clicking on the star.
Please use plain text.
New Developer
banepu
Posts: 14
Registered: ‎08-15-2010
My Device: Blackberry Curve 8330

Re: Delete Persistent Data when the app is uninstalled

No problem! Thanks for your response.


---------
This knowledge sharing is invaluable!
Please use plain text.
Contributor
kaushikdeepak
Posts: 15
Registered: ‎12-22-2010
My Device: Not Specified

Re: Delete Persistent Data when the app is uninstalled

Hii, I am successfully storing and retrieving data in persistance storage area .But when I uninstall the application and again reinstall application than it doesn't delete the previous persistance store data related to project.Old data still in peristance store.I want this store to null.

class MyClass

{

......

.

.

.

.

static RegisterValues registrationValues= new RegisterValues();
static StoreChkBoxValues chkboxState = new StoreChkBoxValues();
public boolean _isApplicationActive;
public static PersistentObject storeChkBoxState;
public static PersistentObject persistRegisterValues;

{
storeChkBoxState = PersistentStore.getPersistentObject( Constants.PERSISTENT_OBJ_KEY );
persistRegisterValues = PersistentStore.getPersistentObject( Constants.PERSISTENT_CELLNO_KEY );
}
public MyClass()
{
int directions = net.rim.device.api.system.Display.DIRECTION_NORTH | net.rim.device.api.system.Display.ORIENTATION_PORTRAIT ;
net.rim.device.api.ui.Ui.getUiEngineInstance().setAcceptableDirections(directions);
QWebService.initialize("http://demo.qbitsystems.com/ifind_blackberry/users");

registrationValues = (RegisterValues)persistRegisterValues.getContents();
chkboxState = (StoreChkBoxValues) storeChkBoxState.getContents();
if(storeChkBoxState.getContents() != null)
{
if(((Boolean)chkboxState.getValue("chkDeviceValue")).booleanValue())
{
backGroundApp = new BackGroundApp();
if(!BackGroundApp.isAlive)
{
backGroundApp.start();
}
}
}
if(registrationValues == null)
{
pushScreen(new LoginScreen());
}
else
{
pushScreen(new HomeScreen());
}
}

.

.

.

.

.



}

Another class RegisterValues is implements persistable interface

import net.rim.device.api.util.*;
import com.qbit.iFind.MyHashTable;
/**
*
*/
public class RegisterValues implements Persistable {
MyHashTable register=new MyHashTable();
public RegisterValues()
{
super();
}
public void setValue(Object key,Object value)
{
register.put(key,value);
}
public Object getValue(Object key)
{
Object value=register.get(key);
return value;
}
}

I also implement custon hashTable as stated in these thread...

http://supportforums.blackberry.com/t5/Java-Development/Delete-Persistent-Storage-on-uninstallation/...

import net.rim.device.api.util.*;
import java.util.Hashtable;

/**
*
*/
public class MyHashTable extends Hashtable implements Persistable {
public MyHashTable()
{
super();
}
}

Still , Old instance save in persitance store after delete the application from simulator.

Can u tell where I have to change..

I m storing this way from another class

.

.

.

.

static RegisterValues registerValues = new RegisterValues();
static
{
persisteRegisterValue = PersistentStore.getPersistentObject( Constants.PERSISTENT_CELLNO_KEY );
synchronized (persisteRegisterValue) {
if ( persisteRegisterValue.getContents() == null ) {

/* registerValues.put("UserName",new String(""));
registerValues.put("Password",new String(""));
registerValues.put("Email",new String(""));
registerValues.put("PurchaseId",new String(""));*/
registerValues.setValue("UserName",new String(""));
registerValues.setValue("Password",new String(""));
registerValues.setValue("Email",new String(""));
registerValues.setValue("PurchaseKey",new String(""));
}
}
}

...

...

...

Please use plain text.
Developer
satish4ru
Posts: 329
Registered: ‎12-17-2010
My Device: Not Specified

Re: Delete Persistent Data when the app is uninstalled

How to remove stored data programatically.. 

Please use plain text.
Developer
peter_strange
Posts: 19,604
Registered: ‎07-14-2008
My Device: Not Specified

Re: Delete Persistent Data when the app is uninstalled

Do you know its ID?

Please use plain text.
Developer
satish4ru
Posts: 329
Registered: ‎12-17-2010
My Device: Not Specified

Re: Delete Persistent Data when the app is uninstalled

Hi peter 

PersistentStore.destroyPersistentObject(TEST_DATA_ID);

 thanks for reply .. 

 

package com.bricato.Storage;

import java.util.Hashtable;

import net.rim.device.api.system.PersistentObject;
import net.rim.device.api.system.PersistentStore;
import net.rim.device.api.util.Persistable;

public final class PersistentDataStore implements Persistable
{

    /* derive TEST_DATA_ID from TestAppDataID1 */
    private long TEST_DATA_ID = 0xbd05d66a3d32db6aL;
    private static PersistentObject testDataBase1;
    private Hashtable hashtable ;
    public long FIRST_ENTRY = 0;
        
    public PersistentDataStore()
    {
    	testDataBase1 = PersistentStore.getPersistentObject(TEST_DATA_ID);
    }

    /* Method to retrieve a saved "string" value */
    public Object getObject(Object key)
    {
        Object result = get(key);
        return (null != result) ? result : null;
    }

    /* Method to save a "string" value */
    public void setObject(Object key, Object object)
    {
        set(key, object);
    }

    /* Method to store an object in persistent data */
    private void set(Object key, Object value)
    {
        synchronized (testDataBase1)
        {
        	hashtable = (Hashtable) testDataBase1.getContents();
            if (null == hashtable)
            {
            	hashtable = new Hashtable();
            }
            hashtable.put(key, value);
            testDataBase1.setContents(hashtable);
            testDataBase1.commit();
        }
    }

    /* Method to retrieve an object from persistent data */
    private Object get(Object key)
    {
        synchronized (testDataBase1)
        {
        	hashtable = (Hashtable)testDataBase1.getContents();
	        if (null != hashtable && hashtable.size() != 0)
	        {
	            return hashtable.get(key);
	        } else
	        {
	            return null;
	        }
        }
    }
    
    public void removeData(){
    	PersistentStore.destroyPersistentObject(TEST_DATA_ID);
    }
    
}

 Here this is the my code i am using i am getting nonPersistentObjectException at  testDataBase1.commit(); testDataBase1.commit(); statement  please help me out.. 

Please use plain text.
Developer
satish4ru
Posts: 329
Registered: ‎12-17-2010
My Device: Not Specified

Re: Delete Persistent Data when the app is uninstalled

i have cretaed a new thread for this issue please give reply .. 

 

i am not understanding the problem why i amgetting like this.. 

Please use plain text.