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
Contributor
viviprost
Posts: 13
Registered: ‎02-18-2013
My Device: BOLD 9900
My Carrier: Developer

Re: Persistent Storage

Ok.

 

I tried to do that :

 

 MessageListDemoStore()
    {
    	_inboxMessages = new ReadableListImpl();
        _deletedMessages = new ReadableListImpl();
        _indicator = ApplicationIndicatorRegistry.getInstance().getApplicationIndicator();
        
        if(_instance == null)
        {
        	PersistentObject _store = PersistentStore.getPersistentObject(MSG_KEY);
        	
        	synchronized(_store)
        	{
        		_instance = (Vector) _store.getContents();
        		
        		if(_instance == null)
        		{
        			_instance = new Vector();
        			_store.setContents(_store);
        			_store.commit();
        		}
        	}
        }

 

and i use this constructor like that :

 

public void execute(ReadOnlyCommandMetadata metadata, Object context) 
            {
                // Create a new message
                DemoMessage message = new DemoMessage();
                message.setSender("John Smith");
                message.setSubject("Hello from John");
                message.setMessage("toto est la");
                message.setReceivedTime(System.currentTimeMillis());

                // Store message 
                MessageListDemoStore messageStore = new MessageListDemoStore();
                synchronized(messageStore)
                {
                    messageStore.addInboxMessage(message);
                }

                Dialog.alert("Unread message was added to inbox");

                // Notify folder
                ApplicationMessageFolder inboxFolder = messageStore.getInboxFolder();
                inboxFolder.fireElementAdded(message, true);                
            }
        }));

 And in my inbox message  there is :

 

void addInboxMessage(DemoMessage message)
    {
        _inboxMessages.addMessage(message);

        if(message.isNew())
        {
            // Update indicator                
            _indicator.setValue(_indicator.getValue() + 1);
            _indicator.setNotificationState(true);
            if(!_indicator.isVisible())
            {
                _indicator.setVisible(true);
            }
        }
        
        _store.commit();
    }

 

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

Re: Persistent Storage

[ Edited ]

Maybe it is just me, but I'm looking at this code trying to figure out what it is actually storing, if anything.  And I am confused.

 

As Simon said, you need to think about how and what you are storing. 

 

Let us take a simple example.  Say you have a EditField on a Screen and you want to retain this value as it is a Setting you use in the Application.  You can't store the EditField because it is not Persistable. 

 

Instead you extract from it the important details - the text - and that is all that is saved.  It can be saved as a String, because String is Persistable. 

 

To do this you have create some processing to 'store' the EditField and to recreate the EditField.  In this processing, when you want to save the EditField, you would just extract the text and save the text.  When you want to restore the EditField, you will just create a new EditField, and get the text from your persisted object and put that in an EditField that you have just created.  The end result to the user is that you have saved and restored the EditField.  But in fact you have just saved the text. 

 

You have to do similar processing for all non persistable classes.

 

So you need to think about all the data you want to save, figure out how to put it into a persistable Object and how to get it back from there.  And also important is when you do this.  This is the 'model' that Simon was talking about. 

Please use plain text.
Contributor
viviprost
Posts: 13
Registered: ‎02-18-2013
My Device: BOLD 9900
My Carrier: Developer

Re: Persistent Storage

Thanks Peter_Strange.

 

Ok, I want to save messages.

The sender, the subject and the text are in the DemoMessage Object.

So i could save the DemoMessage object, no ?

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

Re: Persistent Storage

Sorry no idea, I can't see a DemoMessage Object.  I see you use one, but I can't see the associated class. 

 

The first quesiton you have to ask, is does this Object implement Persistable?  If it doesn't. can it?  If not, then you can't save it and you will have to extract the data to save, and recreate it to restore. 

Please use plain text.