Exception using findById on Data API with Android SDK

I get an exception with the following code:
void findById( String id, int relationsDepth, AsyncCallback<E> responder )

Android SDK version : Backendless SDK v1.10 for Android, released 08.11.2014.

Using the following “TEST code” I get a java.lang.ClassCastException at STEP 3.

STEP 1 : E findById( String id, int relationsDepth ) throws BackendlessException

STEP 2 : void findById( String id, AsyncCallback<E> responder )

STEP 3 : void findById( String id, int relationsDepth, AsyncCallback<E> responder )

TEST code :

public boolean dataRelationsAsync() {

    final CountDownLatch signal = new CountDownLatch(1);

    mRet = false;



    final RelationsAddress address = new RelationsAddress();

    address.setStreet("TN 55");

    address.setCity("Lynchburg");

    address.setState("Tennessee");



    final RelationsContact owner = new RelationsContact();

    owner.setName("Jack Daniels");

    owner.setAge(147);

    owner.setPhone("777-777-777");

    owner.setTitle("Favorites");

    owner.setAddress(address);



    RelationsPhoneBook phoneBook = new RelationsPhoneBook();

    phoneBook.setOwner(owner);

    final RelationsPhoneBook savedPhoneBook = Backendless.Persistence

            .save(phoneBook);



    // STEP 1

    RelationsPhoneBook findById1 = Backendless.Persistence.of(

            RelationsPhoneBook.class).findById(

            savedPhoneBook.getObjectId(), 2);



    // STEP 2

    Backendless.Persistence.of(RelationsPhoneBook.class).findById(

            savedPhoneBook.getObjectId(),

            new AsyncCallback&lt;RelationsPhoneBook&gt;() {

                public void handleResponse(RelationsPhoneBook findById2) {

                    mRet = true;

                    signal.countDown();

                }



                public void handleFault(BackendlessFault fault) {

                    Log.e(TAG, fault.toString());

                    mRet = false;

                    signal.countDown();

                }

            });



    // STEP3

    Backendless.Persistence.of(RelationsPhoneBook.class).findById(

            savedPhoneBook.getObjectId(), 2,

            new AsyncCallback&lt;RelationsPhoneBook&gt;() {

                public void handleResponse(RelationsPhoneBook findById2) {

                    mRet = true;

                    signal.countDown();

                }



                public void handleFault(BackendlessFault fault) {

                    Log.e(TAG, fault.toString());

                    mRet = false;

                    signal.countDown();

                }

            });



    try {

        signal.await();

    } catch (InterruptedException e) {

        e.printStackTrace();

    }

    return mRet;

}

Hi Simon,

Could you try adding the following code before you issue any API calls:

Backendless.Persistence.mapTableToClass( “RelationsContact”, RelationsContact.class );
Backendless.Persistence.mapTableToClass( “RelationsAddress”, RelationsAddress.class );

I assume the table names are “RelationsContact” and “RelationsAddress”.

Regards,
Mark

I got some word back from the engineer:

Backendless.Persistence.mapTableToClass( "RelationsContact", RelationsContact.class );
Backendless.Persistence.mapTableToClass( "RelationsAddress", RelationsAddress.class );

The above two lines didn’t help resolve the issue but the following did:

Backendless.Persistence.mapTableToClass( "RelationsPhoneBook", RelationsPhoneBook.class );

The sync API calls work okay it’s just the async calls the engineer is having the exception with now. Is this perhaps related to the fact some async calls have been disabled, or if not will you be making it so the async method call will also work correctly if the mapTableToClass code is done correctly?

[OK] E findById( String id, int relationsDepth ) throws BackendlessException
[EXCEPTION] void findById( String id, int relationsDepth, AsyncCallback&lt;E&gt; responder )

I’ve attached the source files for reference/

RelationsClass.zip (1.21kB)

Hi, Simon,

In your code sample given above you already have an async findById call. Is it still failing after adding those three mappings suggested by Mark?
I downloaded the classes you gave and reproduced your problem. But after adding the mappings I don’t get any exception. So in case you still have any problem, please, provide some more code you use in order for us to test your issue thoroughly.

Regards,
Sergey

Hi Sergey. I’m working with Simon.

The following code had worked to avoid the exception;

Backendless.Persistence.mapTableToClass( "RelationsPhoneBook", RelationsPhoneBook.class );

But, we’d like to know whether this is temporary workaround or permanent one.
We’ are working to spread the Backendless in Japan. (Please confirm to Mark about our activity.)

So,
If above code is permanent workaround, we expect you to add descrption to your document.
If it is a sort of bug, and above is temporary workaround, we would like to wait for software update.

#I’m not good at writing English. Please bear with my mistake.

Regards,
Niro.

Niro,

This does not make any sense… Could you please attach the source code for the test AND screenshots of the following table’s schema:

RelationsPhoneBook
RelationsContact
RelationsAddress

Thanks,
Mark

Hi Mark,

I’ll jump back intp the conversation here. The actual exception issue has been resolved but there was a few points/questions that the engineer wanted to clarify which may have not being properly conveyed in my translation.

To sum up;
The original suggestion from BE was these 2 lines:

Backendless.Persistence.mapTableToClass( "RelationsContact", RelationsContact.class );
Backendless.Persistence.mapTableToClass( "RelationsAddress", RelationsAddress.class );

but the following line that he figured on his own did solve the issue:

Backendless.Persistence.mapTableToClass( "RelationsPhoneBook", RelationsPhoneBook.class );

The questions he still has are:

  1. Is the line he figured the correct answer to solve the issue?

  2. If are developers were to encounter similar issues, is the official reply to use the mapTableToClass method?

  3. Why is it that the sync API method way works without the mapTableToClass line but the async API method way with exactly the same parameters? Is this a bug?

  4. There is no mention of mapTableToClass in the documentation that he could find so it might be worth adding a note about it.

As you requested, we’ll be getting the source code, screenshots and schemas off him to help you understand exactly what he’s working with.

Regards,
Simon.

Hi Simon,

The 1.12 release of the SDK was updated again with a fix for this issue. The way it works now is how I described earlier - only the mappings for the child entities are needed. Please see my responses below:

  1. Is the line he figured the correct answer to solve the issue?
    No, the correct way to solve the problem is to establish the mappings between the related entity classes and the corresponding tables.

  2. If are developers were to encounter similar issues, is the official reply to use the mapTableToClass method?
    Yes.

  3. Why is it that the sync API method way works without the mapTableToClass line but the async API method way with exactly the same parameters? Is this a bug?
    That was the bug. The “context” of the response class is established for both calls (sync and async). This is the “of( Foo.class )” part. The async call was not passing that context into the invocation handling logic.

  4. There is no mention of mapTableToClass in the documentation that he could find so it might be worth adding a note about it.
    Yes, many changes in the doc are on the way to the production server.

Regards,
Mark