سؤال

I am developing a BlackBerry App and saving a lot of values as String and boolean to the Persistent Store. I am aware of the fact that String and boolean values do not get deleted from Persistent when an App is deleted/uninstalled from the handset. I am also aware of the fact that in order to delete these values I need to store this in a "project" specific class. I have been struggling with it and so would want a temporary work around. I am saving a boolean value in order to determine which screen the App should load it.

PersistentStoreHelper.persistentHashtable.put("flagged",Boolean.TRUE); 

if (PersistentStoreHelper.persistentHashtable.containsKey("flagged")) 
        {
           boolean booleanVal = ((Boolean) PersistentStoreHelper.persistentHashtable.get("flagged")).booleanValue();
           if (booleanVal==true)
           {
              pushScreen(new MyScreen());
           }
        }
        else
        {
            pushScreen(new MyScreen(false));
        }

Is it possible to store this boolean value as an Object so that it gets deleted when the App is uninstalled/deleted. Please help as well as comment if I am missing out on anything.

هل كانت مفيدة؟

المحلول

Once again, I'd recommend you change your PersistentStoreHelper to this version online.

You certainly can get Boolean and String values to be deleted from the persistent store when your app is uninstalled, but they need to be inside an object that can only exist in your app.

For example:

 PersistentStoreHelper store = PersistentStoreHelper.getInstance();
 store.put("flagged", Boolean.TRUE);
 // commit will save changes to the `flagged` variable
 store.commit();

and then retrieve it later with:

 PersistentStoreHelper store = PersistentStoreHelper.getInstance();
 boolean isFlagged = ((Boolean)store.get("flagged")).booleanValue();

The key that makes this work is inside my PersistentStoreHelper class, it saves everything inside a subclass of Hashtable that's unique to my/your app (MyAppsHashtable). You need to store your String or Boolean objects inside that app-unique Hashtable subclass, not in a normal java.util.Hashtable.

Again, please make this easy on yourself, and use the code I posted.

Note: also, you probably know this, but you may need to reboot your device to see the app, and its persistent store data, fully deleted.

Update

If you had changed the original PersistentStoreHelper class that I had posted online, because you wanted access to the containsKey() method, or other methods in the Hashtable class, you can solve that problem by simply adding code like this:

public boolean containsKey(String key) {
    return persistentHashtable.containsKey(key);
}

to the PeristentStoreHelper class. Please don't make persistentHashtable a public static member. As you need to use more Hashtable methods, just add wrappers for them, like I show above with containsKey(). Of course, you can achieve the same thing as containsKey() by simply using this code:

 boolean containsFlagged = (store.get("flagged") != null);

Update 2

If you get stuck with old persistent data, that you need to clean out, you can modify PersistentStoreHelper to detect and correct the situation, like this (hattip to @adwiv for suggestions):

   private PersistentStoreHelper() {
      persistentObject = PersistentStore.getPersistentObject(KEY);

      Object contents = persistentObject.getContents();
      if (contents instanceof MyAppsHashtable) {
         persistentHashtable = (MyAppsHashtable)contents;
      } else {
         // store might be empty, or contents could be the wrong type
         persistentHashtable = new MyAppsHashtable();
         persistentObject.setContents(persistentHashtable);
         persistentObject.commit();
      }
   }
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top