alex-sh
(Alex Sh)
March 13, 2016, 9:43pm
1
Hello,
It is continue from my previous question but i desired to start the new one because it has slightly different issue.
I’m trying to retrieve the related data to the BackendlessUser.
First i did it asynchronously like this:
List<String> rels = new ArrayList<String>();
rels.add( "MyTables" );
Backendless.Data.of( BackendlessUser.class ).loadRelations ( loggedInUser, rels, new AsyncCallback<BackendlessUser>()
{
@Override
public void handleResponse( BackendlessUser response )
{
MyTable[] beTable = (MyTable[])response.getProperty( "MyTables" );
}
@Override
public void handleFault( BackendlessFault fault )
{
}
} );
It works fine, but it takes some time.But then i found that it is possible to Autoload (checking the option in the console), the related data, and tried to do this:
MyTable[] beTable = (MyTable[])loggedInUser.getProperty( "MyTables" );
but it crashes with exception:java.lang.ClassCastException: java.util.HashMap cannot be cast to com.myapp.MyTable.
What i’m doing wrong ?
Hi Alex,
Please add the following line of code before you retrieve any data (perhaps you could do it right after the Backendless.initApp call:
Backendless.Data.mapTableToClass( “MyTables”, MyTables.class )
Also, please keep in mind that for the user objects which do not have any related “MyTables” objects, the cast will fail. Here’s the proper way to do it:
MyTable[] beTable;
Object[] myTablesAsObjects = (Object[])loggedInUser.getProperty( "MyTables" );
if( myTablesAsObjects != null && myTablesAsObjects.length > 0 )
beTable = (MyTable[])myTablesAsObjects;
Regards,
Mark
alex-sh
(Alex Sh)
March 14, 2016, 7:02pm
3
It works the way you wrote.
Thank you Mark.
allen-luo
(Allen Luo)
April 8, 2016, 4:44pm
4
Hey Mark,
For some reason that logic is not working in my case:
with the following code, I get java.lang.ClassCastException: java.util.ArrayList cannot be cast to java.lang.Object[] when I do the first cast.
BackendlessUser user = Backendless.UserService.CurrentUser();
if (user != null) {
Object[] objects = (Object[]) user.getProperty("reservations");
if (objects != null && objects.length>0) {
Reservation[] reservations = (Reservation[])objects;
for(int i=0; i<reservations.length; i++){
Reservation res = reservations;
if (res.deal.getObjectId().equals(deal.getObjectId())) {
reservation = res;
break;
}
}
}
}
Hi Allen,
Do you have a stack trace for the error? Are there any auto-load relations anywhere in the hierarchy of the “reservations” column?
Mark