I started out with RMS lately(well last hour only) and laid out methods for my class to use RMS for data persistency. The MIDlet works fine both in my S40 Nokia cell & the emulator(Sun Java) as long as the App is not closed.
Steps
1. I add record to my class
2. I update database
3. I erase the object(list)
4. I redeem the object(list) from the database
Everything works fine till this point. But as soon as I close the App, for some strange reason the persistent RMS is persistent no more and upon opening I get an Exception saying no record exists.
My Object's Class' methods:
.
.
.
private RecordStore dataStore;
public void openRecordStore(boolean canCreate) throws RecordStoreException
{
dataStore = RecordStore.openRecordStore("NX Record Master", canCreate);
}
public void closeRecordStore() throws RecordStoreException
{
dataStore.closeRecordStore();
}
public void deleteRecordStore() throws RecordStoreException
{
RecordStore.deleteRecordStore("NX Record Master");
}
private Credentials fromByteArray( byte[] data) throws IOException
{
ByteArrayInputStream bin = new ByteArrayInputStream(data);
DataInputStream din = new DataInputStream( bin );
Credentials currUser=new Credentials();
currUser.setEmail(din.readUTF());
currUser.setUserName(din.readUTF());
currUser.setPassWord(din.readUTF());
currUser.setComment(din.readUTF());
din.close();
return currUser;
}
private byte[] toByteArray(Credentials currUser) throws IOException
{
ByteArrayOutputStream bout = new ByteArrayOutputStream();
DataOutputStream dout = new DataOutputStream( bout );
dout.writeUTF( currUser.getEmail() );
dout.writeUTF( currUser.getUserName() );
dout.writeUTF( currUser.getPassWord() );
dout.writeUTF( currUser.getComment() );
dout.close();
return bout.toByteArray();
}
public boolean storeDataRecord()
{
for(int i=0; i<userCount; i++)
{
try
{
byte[] data = this.toByteArray(this.getCredentials()[i]);
dataStore.addRecord( data, 0, data.length );
}
catch( RecordStoreFullException e )
{
return false;
}
catch( RecordStoreException e )
{
// handle the RMS error here
e.printStackTrace();
}
catch( IOException e )
{
// handle the IO error here
e.printStackTrace();
}
}
return true;
}
public boolean retrieveDataRecord()
{
int maxRecords=0;
userCount=0;
try
{
maxRecords=dataStore.getNumRecords();
}
catch(Exception e)
{
e.printStackTrace();
}
for(int i=1; i<=maxRecords; i++)
{
try
{
Credentials targetUser, thisUser;
thisUser = this.fromByteArray(dataStore.getRecord(i));
targetUser = this.getCredentials()[getCount()];
if(targetUser==null)
targetUser = new Credentials();
this.getCredentials()[this.getCount()].setEmail(thisUser.getEmail());
this.getCredentials()[this.getCount()].setUserName(thisUser.getUserName());
this.getCredentials()[this.getCount()].setPassWord(thisUser.getPassWord());
this.getCredentials()[this.getCount()].setComment(thisUser.getComment());
this.addCount();
}
catch( RecordStoreException e )
{
// handle the RMS error here
e.printStackTrace();
//return false;
}
catch( IOException e )
{
// handle the IO error here
e.printStackTrace();
//return false;
}
}
return true;
}
And the wrapping is provided by the functions:
public boolean saveUserData()
{
try
{
User.openRecordStore(true);
if(User.storeDataRecord())
infoBox("Records have been updated.", getMainMenu());
else
errorBox("Error saving. Out of memory.", getMainMenu());
User.closeRecordStore();
}
catch(Exception e)
{
errorBox("Record Store cannot be opened.", getMainMenu());
return false;
}
return true;
}
public boolean fetchUserData()
{
try
{
User.openRecordStore(false);
if(User.retrieveDataRecord())
infoBox("Records have been loaded.", getMainMenu());
else
errorBox("Error fetching records.", getMainMenu());
User.closeRecordStore();
}
catch(Exception e)
{
errorBox("Record Store cannot be opened.", getMainMenu());
return false;
}
return true;
}
And as stated everything is fine till I infact close the application and upon reopening I get "Record Store cannot be opened."
According to the Sun Documentation Record Stores are Persistent but I can't figure out why I'm not getting the desired result.
Thanks,
Nisheeth Barthwal