When trying to fetch a list of Contact table relationships from a 1->Many user property called ‘contacts’, sometimes the resulting Java array is of the correct type and other times the same block of code yields a list of type HashMap.
Take the following block for example:
Object[] c = (Object[])user.getProperty("contacts");
Contact[] cTyped = new Contact[c.length];
System.arraycopy(c, 0, cTyped, 0, c.length);
This block will sometimes correctly yield an array of Contacts, but other times the following exception is thrown:
java.lang.ArrayStoreException: source[0] of type java.util.HashMap cannot be stored in destination array of type Contact[]
Upon app launch, I am calling the function Backendless.Data.mapTableToClass(“Contact”, Contact.class); to ensure proper mapping. But this error still happens, seemingly at random. Although, right now it is happening consistently, which is why I’m finally writing a support topic about it.
Server sends the same response every time, so it must be something on the client side. Could you try moving the call to map table to class (Backendless.Data.mapTableToClass) closer to the code where the user object is loaded?
Looks good (the backreference to BackendlessUser is questionable, but should not be a problem). Could you please check if it works in a completely isolated app where all you do is login and then get the contacts property?
The BackendlessUser in this case is not a back reference, it is a different user that the owning user has added to their contacts list. The purpose of the Contact table is to act as a list of users + additional information (in this case userScore) that relates to the owner’s relationship with the Contact user.
Okay, that makes sense. Could you please run the test I described and let us know? If it works in that case, there must be something else in the code that messes up the mapping.
Moving it out works. Its yielding contacts correctly. What would cause Backendless SDK to lose its mappings at some point during code execution? Should mapTableToClass just be called before any data retrieval operation? I know calling it in Application.onCreate works because this isn’t a consistent bug. Just curious if there is anything known to cause the mappings to be lost.
The mapping is stored in a static variable. It is known for Android to wipe out static values, perhaps this is the case here. Multiple calls to establish the mapping are harmless and, yes, you can call it before every data retrieval operation.
it would be interesting to run the following test - you can check if the mapping is still in place by making the following call:
I’m continuing to get this error in my main app, even if I call mapTableToClass before fetching the user. For example, here is how I’m getting my user when they were previously logged in:
public static void VerifyLogin(@NonNull final AuthResponse callback) {
String loggedInUser = Backendless.UserService.loggedInUser();
if(loggedInUser == null || loggedInUser.isEmpty()) {
callback.handleResponse(null, false, "No previous session found. Please login.");
} else {
Backendless.Data.mapTableToClass("Contact", Contact.class);
Backendless.Data.of(BackendlessUser.class).findById(loggedInUser, 2,
new AsyncCallback<BackendlessUser>() {
@Override
public void handleResponse(BackendlessUser response) {
Backendless.UserService.setCurrentUser(response);
...
I need to have a way to reproduce the issue. It works in an empty app for us as well. If you’d like you can package your app and share with us and we will debug it there.