0

I am facing an issue in storing data in persistence store,i am trying to store events for different dates in persistence store but the data is getting overridden the code is :

public ListEventScreen(Vector v,String timezone) { 
    for(int i=0;i<v.size();i++){
        EventBean bean=(EventBean)v.elementAt(i);
        //a normal label in the app, just to display text, anchored left
        LabelField label = new LabelField(bean.getSummary(),LabelField.FIELD_LEFT);
        //add the label to the screen
        add(label);   
        saveUserInfo(v);
    } 
}

public void saveUserInfo(Vector vectorData){
    // static{
        store = PersistentStore.getPersistentObject( 0x1dfc10ec9447eb14L );
        synchronized(store) {
            store.setContents(vectorData); 
            store.commit();
        }
    //}
}

Please let me know what has to be changed.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
sheetal_r oswal
  • 155
  • 2
  • 2
  • 10

1 Answers1

1

Every time you call store.setContents(), the current contents of the persistentStore are overwritten with the Vector you are passing in. You need to make sure you are loading the previous events that were already in the persistentStore into your Vector before then adding new events into that Vector that you are then saving.

You are also calling saveUserInfo() on every iteration of your loop in ListEventScreen(). You should be calling it outside of the loop instead.

I would do something like this:

public ListEventScreen(Vector v,String timezone) { 
    Enumeration e = v.elements();;
    while (e.hasMoreElements()){
        EventBean bean = (EventBean) e.nextElement();
        //a normal label in the app, just to display text, anchored left
        LabelField label = new LabelField(bean.getSummary(),LabelField.FIELD_LEFT);
        //add the label to the screen
        add(label);   
    } 
}

public void loadUserInfo(Vector vectorData){
    // static{
        store = PersistentStore.getPersistentObject( 0x1dfc10ec9447eb14L );
        synchronized(store) {
            Vector v = (Vector) store.getContents(); 
            Enumeration e = v.elements();
            while (e.hasMoreElemens){
                vectorData.add(e.nextElement());
            }
        }
    //}
}

public void saveUserInfo(Vector vectorData){
    // static{
        store = PersistentStore.getPersistentObject( 0x1dfc10ec9447eb14L );
        synchronized(store) {
            store.setContents(vectorData); 
            store.commit();
        }
    //}
}

.

{
    Vector v = new Vector();
    loadUserInfo(v);
    ListEventScreen(v, ...);
    ... modify v contents as needed ...
    saveUserInfo(v);
}

If you do not mind changing the format of your persistent store contents, I would wrap the store in a singleton class instead:

public class EventBeans extends Vector implements Persistable
{   
    private static final long persistKey = 0x1dfc10ec9447eb14L;

    private static EventBeans _instance = null;
    private static PersistentObject _persist = null;

    static{
        _persist = PersistentStore.getPersistentObject(persistKey);
        _instance = (EventBeans) _persist.getContents();
        if (_instance == null){
            _instance = new EventBeans();
            _persist.setContents(_instance);
            _persist.commit();
        }
    }

    private EventBeans(){       
        super();
    }

    public static EventBeans getInstance(){
        return _instance;
    }

    public static synchronized void save(){
        _persist.commit();
    }
}

.

{
    Vector v = EventBeans.getInstance();
    ListEventScreen(v, ...);
    ... modify v contents as needed ...
    EventBeans.save();
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • ,i am not getting when to call loadUserInfo method the snippet u added --{ Vector v = new Vector(); loadUserInfo(v); ListEventScreen(v, ...); ... modify v contents as needed ... saveUserInfo(v); } is not understood – sheetal_r oswal Mar 13 '12 at 05:34
  • If you want to preserve the events you have already saved in the store, you have to read them from the store and put them into the Vector that you save back into the store. When do you call that? Before you make updates to the store. What is unclear about that? – Remy Lebeau Mar 13 '12 at 06:18