In the afterCreate of one of my objects, I get a user from that object.
A user has a property named friends that is a 1-to-Many relation to other BackendlessUsers.
I am trying to add users to that relation, but when I try to obtain friends as a list it says I can’t cast type Object to List. @Override
public void afterCreate( RunnerContext context, Friendship friendship, ExecutionResult<Friendship> result ) throws Exception
{
BackendlessUser user = friendship.getMember();
List<BackendlessUser> friends = (List<BackendlessUser>) user.getProperty(“friends”);
}
Getting back:
[[Ljava.lang.Object; cannot be cast to java.util.ArrayList] <[Ljava.lang.Object; cannot be cast to java.util.ArrayList>
Server reported an error: FAULT = '0' [[Ljava.lang.Object; cannot be cast to [Lcom.backendless.BackendlessUser;] <[Ljava.lang.Object; cannot be cast to [Lcom.backendless.BackendlessUser;>
So, I’m still wondering how I can properly obtain a relation property from my user.
The friendship object has the property members which is a relation to many BackendlessUsers. And my BackendlessUsers have a property named friends that is a relation to other BackendlessUsers.
To reiterate, the issue is when I try to access the “friends” property on my User, it is saying I cannot cast a java.lang.Object to a List or any other type of Array of BackendlessUsers.
Note: the friends property of the user I am working with is not empty.
When a user’s relation property has no data, it will always be empty Object[] (not castable). The proper way to get relational data out of a user object is:
Get it as Object[]
Check if the array has length > 0
If (2) is true, then you can case it to an array of the expected type.
Yeah, that definitely makes sense. I’m just wondering now why my currentUser’s friends show 0 objects before saving the friendship, whereas the other user properly shows it has an object in friends.
When you’re calling save() on the Friendship object, your event handler will get the same object you’re saving. Which means when your event handler code does this:
Yes, I am getting my current user from login, yet its friends property has 0 objects (right before saving the friendship):
Even though it definitely has at least one friend:
So I don’t see how that purely deals with only the “logic” of my app? According to this it should show friends to have that object much like the other users that I query for show.
As far as I can tell this seems like it could be an issue with related properties on the currentUser. Otherwise, I’m not sure why it’s showing 0 objects if login is supposed to be returning to me a current user with all of its related properties.
Sorry if my questions are getting annoying; I’m just getting acquainted with Backendless since working with another BaaS for so long. So just trying to figure out these fundamental concepts and sometimes it’s hard to tell if it’s something I’m doing wrong or potentially something on the other end. Appreciate your help as always.