09-21-2010 11:07 PM
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!
Solved! Go to Solution.
09-21-2010 11:40 PM
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.)
09-22-2010 11:27 PM
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?
09-25-2010 10:24 PM
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.)
09-28-2010 12:14 PM
No problem! Thanks for your response.
05-18-2011 07:35 AM
10-14-2011 02:45 AM
How to remove stored data programatically..
10-14-2011 06:15 AM
Do you know its ID?
10-14-2011 06:19 AM
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..
10-14-2011 06:30 AM
i have cretaed a new thread for this issue please give reply ..
i am not understanding the problem why i amgetting like this..