Delete Persistent Store data when App is uninstalled/deleted

♀尐吖头ヾ 提交于 2020-02-02 13:04:53

问题


I have a BlackBerry application that starts (App load) with a Registration screen when the App is first installed. Later, the App will load with the home screen. Registration screen only appears on first load. I am achieving this by storing a boolean value in PersistentStore. If the value exists, then Registration screen will not appear.

PersistentStoreHelper.persistentHashtable.put("flagged",Boolean.TRUE);
PersistentStoreHelper.persistentObject.commit();
UiApplication.getUiApplication().pushScreen(new MyScreen());

I am aware of the fact that in order to delete the Persistent Store on deleting/uninstalling the App, I have to make the Hashtable a subclass of my own and therefore I have declared the Hashtable in a separate class:

public class PersistentStoreHelper extends Hashtable implements Persistable{

    public static PersistentObject persistentObject;
    public static final long KEY = 0x9df9f961bc6d6daL;
    public static Hashtable persistentHashtable;

}

However this has not helped and the boolean value of flag is not cleared from PersistentStore. Please advice.


EDIT: When I change the above PersistentStoreHelper class to

public static PersistentObject persistentObject =
    PersistentStore.getPersistentObject(KEY);

and remove

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

from class B where boolean value is being saved, I observe that the boolean value is removed every time the App is closed. This should not happen and the value should only be removed in case the App is deleted/uninstalled. Any pointers?


回答1:


The way this works is that the BlackBerry OS looks at the objects you are storing in the PersistentStore. If it recognizes that those objects can only be used by your app, then it will delete them when you uninstall the app. However, if the classes of the stored objects are classes that are used by other apps, then your data will not be deleted.

You have declared your helper class like this:

public class PersistentStoreHelper extends Hashtable implements Persistable{

but the helper class is not what is being stored. Your helper class is just a helper, that stores other things for you. In your case, it is storing this:

public static Hashtable persistentHashtable;

but, that object is of type java.util.Hashtable, which is a class used by many apps. So, it won't be deleted when you uninstall your app. What you should do is something like this:

public class PersistentStoreHelper implements Persistable {  // so inner class = Persistable

    public static PersistentObject persistentObject;
    public static final long KEY = 0x9df9f961bc6d6daL;
    /** 
      * persistentHashtable is now an instance of a class that
      * only exists in your app 
      */
    public static MyAppsHashtable persistentHashtable;

    private class MyAppsHashtable extends Hashtable implements Persistable {
        // don't need anything else ... the declaration does it all!
    }
}

I can't see it here, but I'm assuming that somewhere you have this code:

persistentObject = PersistentStore.getPersistentObject(KEY);

and then when you want to save the data back to the store, you're doing something like this;

persistentHashtable.put("SomeKey", someNewData);
persistentObject.setContents(persistentHashtable);
persistentObject.commit();

just adding data to the persistentHashtable doesn't save it (permanently). Hopefully, you already had that part somewhere.


Note: if you make these changes, don't expect this line of code to work, the next time you run your app:

persistentHashtable = (MyAppsHashtable)persistentObject.getContents();

because the last version of your code did not use the MyAppsHashtable class, so the loaded data won't be of that type. This is one reason that it's important to get this right the first time. In general, I always wind up saving data in the PersistentStore that's contained in one top level Hashtable subclass, that implements Persistable. I may later change what goes in it, but I won't ever change the signature of that top-level storage object. Hopefully, you haven't released your app already.


Update: In response to your comment/question below:

if (PersistentStoreHelper.persistentObject.getContents() == null) {   
    PersistentStoreHelper.persistentHashtable = new MyAppsHashtable();    
    PersistentStoreHelper.persistentObject.setContents(PersistentStoreHelper.persist‌entHashtable); 
} else { 
    PersistentStoreHelper.persistentHashtable = 
        (MyAppsHashtable)PersistentStoreHelper.persistentObject.getContents(); 
} 


来源:https://stackoverflow.com/questions/15743380/delete-persistent-store-data-when-app-is-uninstalled-deleted

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!