Troubleshooting guide

82
BlackBerry Java Development Environment Development Guide
Using SyncObjects
Code sample: Using a SyncCollection to back up data over the wireless
network
Example: OTABackupRestoreContactCollection.java
/*
* OTABackupRestoreContactCollection.java
*
* AUTO_COPYRIGHT_SUB_TAG
*/
package com.rim.samples.device.otabackuprestoredemo;
Task Steps
Retrieve SyncObjects from
the SyncCollection.
> Implement the getSyncObjects() method.
public SyncObject[] getSyncObjects()
{//Retrieve the contact data.
SyncObject[] contactArray = new SyncObject[_contacts.size()];
for (int i = _contacts.size() - 1; i >= 0; --i)
{
contactArray[i] = (SyncObject)_contacts.elementAt(i);
}
return contactArray;
}
Access a specific SyncObject. > Implement the getSyncObject() method, using the _uid variable to retrieve a specific SyncObject.
public SyncObject getSyncObject(int uid)
{
for (int i = _contacts.size() - 1; i >= 0; --i)
{
SyncObject so = (SyncObject)_contacts.elementAt(i);
if ( so.getUID() == uid ) return so;
}
return null;
}
Add a SyncObject to the
SyncCollection.
> Create a method that adds SyncObjects to the PersistentStore object.
public boolean addSyncObject(SyncObject object)
{
// Add a contact to the PersistentStore object.
_contacts.addElement(object);
}
Save a SyncCollection. Before a wireless backup session ends, save the newest SyncCollection data.
Invoke the setContents() and commit()methods.
public void endTransaction()
{
_persist.setContents(_contacts);
_persist.commit();
}