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
New Developer
arkka
Posts: 5
Registered: ‎10-05-2009
My Device: Not Specified

[HELP] MessageListDemo with PersistentStore instead RuntimeStore ??

I've import MessageListDemo from JDE samples, and i can run that project without any problem.
MessageListDemo is using RuntimeStore for store message.

Class that responsible for that: MessageListDemoStore

package com.rim.samples.device.messagelistdemo;

import java.util.Vector;

import net.rim.blackberry.api.messagelist.*;
import net.rim.device.api.collection.ReadableList;
import net.rim.device.api.system.RuntimeStore;


/**
* This class is used to facilitate the storage of messages. For the sake of
* simplicitly, we are saving messages in the device runtime store. In a real
* world situation, messages would be saved in device persistent store and/or
* a mail server.
*/
final class MessageListDemoStore
{
// com.rim.samples.device.messagelistdemo.MessageListDemoStore
private static final long MSG_KEY = 0xcf2b552e0e98a715L;

private static MessageListDemoStore _instance;

private ReadableListImpl _inboxMessages;
private ReadableListImpl _deletedMessages;
private ApplicationMessageFolder _mainFolder;
private ApplicationMessageFolder _deletedFolder;
private ApplicationIndicator _indicator;

// Constructor
MessageListDemoStore()
{
_inboxMessages = new ReadableListImpl();
_deletedMessages = new ReadableListImpl();
_indicator = ApplicationIndicatorRegistry.getInstance().getApplicationIndicator();
}

static MessageListDemoStore getInstance()
{
// Keep messages as singleton in the RuntimeStore.
if( _instance == null )
{
RuntimeStore rs = RuntimeStore.getRuntimeStore();

synchronized( rs )
{
_instance = (MessageListDemoStore) rs.get( MSG_KEY );

if( _instance == null )
{
_instance = new MessageListDemoStore();
rs.put( MSG_KEY, _instance );
}
}
}
return _instance;
}

void setFolders( ApplicationMessageFolder mainFolder, ApplicationMessageFolder deletedFolder )
{
_mainFolder = mainFolder;
_deletedFolder = deletedFolder;
}

ApplicationMessageFolder getInboxFolder()
{
return _mainFolder;
}

ApplicationMessageFolder getDeletedFolder()
{
return _deletedFolder;
}

/**
* User deleted message, move it into deleted folder.
*/
void deleteInboxMessage( DemoMessage message )
{
message.messageDeleted();
_inboxMessages.removeMessage( message );
_deletedMessages.addMessage( message );

// Update indicator.
_indicator.setValue(_indicator.getValue() - 1);
if(_indicator.getValue() <= 0)
{
_indicator.setVisible(false);
}

}

void commitMessage( DemoMessage message )
{
// This empty method exists to reinforce the idea that in a real world
// situation messages would be saved in device persistent store and/or
// on a mail server.
}

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

// Update indicator.
_indicator.setValue(_indicator.getValue() + 1);
if(!_indicator.isVisible())
{
_indicator.setVisible(true);
}
}

/**
* User deleted message from the Deleted folder.
*
* @param message
*/
void deleteMessageCompletely( DemoMessage message )
{
_deletedMessages.removeMessage( message );
}

ReadableListImpl getInboxMessages()
{
return _inboxMessages;
}

ReadableListImpl getDeletedMessages()
{
return _deletedMessages;
}

static class ReadableListImpl implements ReadableList
{
private Vector messages;

ReadableListImpl()
{
messages = new Vector();
}

public Object getAt( int index )
{
return messages.elementAt( index );
}

public int getAt( int index, int count, Object[] elements, int destIndex )
{
return 0;
}

public int getIndex( Object element )
{
return messages.indexOf( element );
}

public int size()
{
return messages.size();
}

void addMessage( DemoMessage message )
{
messages.addElement( message );
}

void removeMessage( DemoMessage message )
{
messages.removeElement( message );
}
}
}

 

 

You can see from the class description comment

 

/**
* This class is used to facilitate the storage of messages. For the sake of
* simplicitly, we are saving messages in the device runtime store. In a real
* world situation, messages would be saved in device persistent store and/or
* a mail server.
*/

 



I need this class using PersistentStore, and it drive me crazy.

This is my MessageListDemoStore

 

 

package com.rim.samples.device.messagelistdemo;

import java.util.Vector;

import net.rim.blackberry.api.messagelist.*;
import net.rim.device.api.collection.ReadableList;
import net.rim.device.api.util.Persistable;
import net.rim.device.api.system.PersistentObject;
import net.rim.device.api.system.PersistentStore;


/**
* This class is used to facilitate the storage of messages. For the sake of
* simplicitly, we are saving messages in the device runtime store. In a real
* world situation, messages would be saved in device persistent store and/or
* a mail server.
*/
final class MessageListDemoStore implements Persistable
{
// com.rim.samples.device.messagelistdemo.MessageListDemoStore
private static final long MSG_KEY = 0xcf2b552e0e98a715L;

private static MessageListDemoStore _instance;

private ReadableListImpl _inboxMessages;
private ReadableListImpl _deletedMessages;
private ApplicationMessageFolder _mainFolder;
private ApplicationMessageFolder _deletedFolder;
private ApplicationIndicator _indicator;

// Constructor
MessageListDemoStore()
{
_inboxMessages = new ReadableListImpl();
_deletedMessages = new ReadableListImpl();
_indicator = ApplicationIndicatorRegistry.getInstance().getApplicationIndicator();
}

static MessageListDemoStore getInstance()
{
// Keep messages as singleton in the RuntimeStore.
if( _instance == null )
{
PersistentObject ps = PersistentStore.getPersistentObject(MSG_KEY);

synchronized( ps )
{
_instance = (MessageListDemoStore) ps.getContents();

if( _instance == null )
{
_instance = new MessageListDemoStore();
ps.setContents(_instance);
ps.commit();
}
}
}
return _instance;
}

void setFolders( ApplicationMessageFolder mainFolder, ApplicationMessageFolder deletedFolder )
{
_mainFolder = mainFolder;
_deletedFolder = deletedFolder;
}

ApplicationMessageFolder getInboxFolder()
{
return _mainFolder;
}

ApplicationMessageFolder getDeletedFolder()
{
return _deletedFolder;
}

/**
* User deleted message, move it into deleted folder.
*/
void deleteInboxMessage( DemoMessage message )
{
message.messageDeleted();
_inboxMessages.removeMessage( message );
_deletedMessages.addMessage( message );

// Update indicator.
_indicator.setValue(_indicator.getValue() - 1);
if(_indicator.getValue() <= 0)
{
_indicator.setVisible(false);
}

}

void commitMessage( DemoMessage message )
{
// This empty method exists to reinforce the idea that in a real world
// situation messages would be saved in device persistent store and/or
// on a mail server.
}

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

// Update indicator.
_indicator.setValue(_indicator.getValue() + 1);
if(!_indicator.isVisible())
{
_indicator.setVisible(true);
}
}

/**
* User deleted message from the Deleted folder.
*
* @param message
*/
void deleteMessageCompletely( DemoMessage message )
{
_deletedMessages.removeMessage( message );
}

ReadableListImpl getInboxMessages()
{
return _inboxMessages;
}

ReadableListImpl getDeletedMessages()
{
return _deletedMessages;
}

static class ReadableListImpl implements ReadableList
{
private Vector messages;

ReadableListImpl()
{
messages = new Vector();
}

public Object getAt( int index )
{
return messages.elementAt( index );
}

public int getAt( int index, int count, Object[] elements, int destIndex )
{
return 0;
}

public int getIndex( Object element )
{
return messages.indexOf( element );
}

public int size()
{
return messages.size();
}

void addMessage( DemoMessage message )
{
messages.addElement( message );
}

void removeMessage( DemoMessage message )
{
messages.removeElement( message );
}
}
}

 



And after i run the project, simulator give me JVM Error  525 bad Persistent Object.
Please, please.. I need your help. Any help will be highly appreciated.

Best regards,

 

Best regards,


Arkka
blog.dhiratara.com
Please use plain text.
Administrator
MSohm
Posts: 14,444
Registered: ‎07-09-2008
My Device: BlackBerry Z30, BlackBerry PlayBook
My Carrier: Bell

Re: [HELP] MessageListDemo with PersistentStore instead RuntimeStore ??

The sample stores a copy of the application itself as a singleton object in the RuntimeStore.  Application instances should not be stored in PersistentStore. 

 

You should store your individual messages in the persistent store instead.  Have a look at the persistentstoredemo for an example on using the persistent store.  You can also find documentation here:

 

http://docs.blackberry.com/en/developers/deliverables/7693/Persistent_data_storage_509562_11.jsp

Mark Sohm
BlackBerry Development Advisor

Please refrain from posting new questions in solved threads.
Problem solved? Click the Accept As Solution button.
Found a bug? Report it using Issue Tracker
Please use plain text.
New Developer
arkka
Posts: 5
Registered: ‎10-05-2009
My Device: Not Specified

Re: [HELP] MessageListDemo with PersistentStore instead RuntimeStore ??

[ Edited ]

hanks for your reply MSohm,

 

Yes.. i've already read those sample and document. But, still didn't give me any clue to implement it to MessageListDemo. :smileyindifferent:

Do i need stored my message on Vector object?

 

please give mesample script on this implementation, to help me understand it. 

your help will be very appreciated.

Message Edited by arkka on 10-08-2009 01:50 PM
Best regards,


Arkka
blog.dhiratara.com
Please use plain text.
Developer
ydaraishy
Posts: 562
Registered: ‎09-30-2009
My Device: Not Specified

Re: [HELP] MessageListDemo with PersistentStore instead RuntimeStore ??

[ Edited ]

You need to make sure that:

* the object you're persisting implements Persistable

* ALL references from the object you are persisting must also implement Persistable, or are "naturally persistable" objects like Vector.

 

See http://www.blackberry.com/developers/docs/4.6.0api/net/rim/device/api/system/PersistentObject.html for more details.

Message Edited by ydaraishy on 10-08-2009 03:27 AM
Please use plain text.
New Developer
arkka
Posts: 5
Registered: ‎10-05-2009
My Device: Not Specified

Re: [HELP] MessageListDemo with PersistentStore instead RuntimeStore ??

thanks for reply ydaraishy

 

* i've add Persistable at ReadableListImpl class

* ReadableListImpl.messages is a Vector

* i Create 2 (two) persistentstore (inbox & deleted folder)

 

but there's another error on ClassCastException

 

this's my script.. please take a look.. 

 

 

/**
* MessageListDemoStore.java
*
* Copyright © 1998-2009 Research In Motion Ltd.
*
* Note: For the sake of simplicity, this sample application may not leverage
* resource bundles and resource strings. However, it is STRONGLY recommended
* that application developers make use of the localization features available
* within the BlackBerry development platform to ensure a seamless application
* experience across a variety of languages and geographies. For more information
* on localizing your application, please refer to the BlackBerry Java Development
* Environment Development Guide associated with this release.
*/

package com.rim.samples.device.messagelistdemo;

import java.util.Vector;

import net.rim.blackberry.api.messagelist.*;
import net.rim.device.api.collection.ReadableList;
import net.rim.device.api.system.PersistentObject;
import net.rim.device.api.system.PersistentStore;
import net.rim.device.api.system.RuntimeStore;
import net.rim.device.api.util.Persistable;


/**
* This class is used to facilitate the storage of messages. For the sake of
* simplicitly, we are saving messages in the device runtime store. In a real
* world situation, messages would be saved in device persistent store and/or
* a mail server.
*/
final class MessageListDemoStore
{
// com.rim.samples.device.messagelistdemo.MessageListDemoStore.DELETED_KEY
private static final long INBOX_KEY = 0x83b848f0be867adcL;
private static final long DELETED_KEY = 0x19979b94e5228ea1L;

private static MessageListDemoStore _instance;

private ReadableListImpl _inboxMessages;
private ReadableListImpl _deletedMessages;
private ApplicationMessageFolder _mainFolder;
private ApplicationMessageFolder _deletedFolder;
private ApplicationIndicator _indicator;

private static PersistentObject psInbox;
private static PersistentObject psDeleted;


// Constructor
MessageListDemoStore()
{
_inboxMessages = new ReadableListImpl();
_deletedMessages = new ReadableListImpl();
_indicator = ApplicationIndicatorRegistry.getInstance().getApplicationIndicator();
}
static MessageListDemoStore getInstance() {
_instance = new MessageListDemoStore();

psInbox = PersistentStore.getPersistentObject(INBOX_KEY);
synchronized (psInbox) {
if (psInbox.getContents() == null) {
psInbox.setContents(new Vector());
psInbox.commit();
}
}
_instance._inboxMessages.messages = new Vector();
_instance._inboxMessages.messages = (Vector) psInbox.getContents();

psDeleted = PersistentStore.getPersistentObject(DELETED_KEY);
synchronized (psDeleted) {
if (psDeleted.getContents() == null) {
psDeleted.setContents(new Vector());
psDeleted.commit();
}
}
_instance._deletedMessages.messages = new Vector();
_instance._deletedMessages.messages = (Vector) psDeleted.getContents();

return _instance;
}


void setFolders( ApplicationMessageFolder mainFolder, ApplicationMessageFolder deletedFolder )
{
_mainFolder = mainFolder;
_deletedFolder = deletedFolder;
}

ApplicationMessageFolder getInboxFolder()
{
return _mainFolder;
}

ApplicationMessageFolder getDeletedFolder()
{
return _deletedFolder;
}

/**
* User deleted message, move it into deleted folder.
*/
void deleteInboxMessage( DemoMessage message )
{
message.messageDeleted();
_inboxMessages.removeMessage( message );
_deletedMessages.addMessage( message );

// Update indicator.
_indicator.setValue(_indicator.getValue() - 1);
if(_indicator.getValue() <= 0)
{
_indicator.setVisible(false);
}

}

void commitMessage( DemoMessage message )
{
// This empty method exists to reinforce the idea that in a real world
// situation messages would be saved in device persistent store and/or
// on a mail server.
}

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

// Update indicator.
_indicator.setValue(_indicator.getValue() + 1);
if(!_indicator.isVisible())
{
_indicator.setVisible(true);
}
}

/**
* User deleted message from the Deleted folder.
*
* @param message
*/
void deleteMessageCompletely( DemoMessage message )
{
_deletedMessages.removeMessage( message );
}

ReadableListImpl getInboxMessages()
{
return _inboxMessages;
}

ReadableListImpl getDeletedMessages()
{
return _deletedMessages;
}

static class ReadableListImpl implements ReadableList, Persistable
{
private Vector messages;

ReadableListImpl()
{
messages = new Vector();
}

public Object getAt( int index )
{
return messages.elementAt( index );
}

public int getAt( int index, int count, Object[] elements, int destIndex )
{
return 0;
}

public int getIndex( Object element )
{
return messages.indexOf( element );
}

public int size()
{
return messages.size();
}

void addMessage( DemoMessage message )
{
messages.addElement( message );
}

void removeMessage( DemoMessage message )
{
messages.removeElement( message );
}
}
}

 

 Thanks again.

 

Best regards,


Arkka
blog.dhiratara.com
Please use plain text.