Casting User (relation) property to List in Java

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] &lt;[Ljava.lang.Object; cannot be cast to java.util.ArrayList&gt; 

I found a similar question here: http://support.backendless.com/t/user-to-user-relations-friend-list-android-example

He said that the following works:

BackendlessUser[] friends = (BackendlessUser[]) user.getProperty("friends"); 

but when I try I get a similar error as before:

Server reported an error: FAULT = '0' [[Ljava.lang.Object; cannot be cast to [Lcom.backendless.BackendlessUser;] &lt;[Ljava.lang.Object; cannot be cast to [Lcom.backendless.BackendlessUser;&gt; 

So, I’m still wondering how I can properly obtain a relation property from my user.

and here’s what the friends property on my user looks like from debugging (at the bottom of the photo):
http://support.backendless.com/public/attachments/39bd60578c2b486ca766228031860f8b.png&lt;/img&gt;

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.

39bd60578c2b486ca766228031860f8b.png

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:

  1. Get it as Object[]
  2. Check if the array has length > 0
  3. If (2) is true, then you can case it to an array of the expected type.

Regards,
Mark

So I tried what you suggested:

Object[] objects = (Object[]) user.getProperty("friends");


if (objects.length > 0) {
    BackendlessUser[] friends = (BackendlessUser[]) objects;
}

But it is saying objects.length is 0 even though there is an object in there for the user of whom I am getting it from:

http://support.backendless.com/public/attachments/917064ae69146ef4ce868d03a2687412.png&lt;/img&gt;

http://support.backendless.com/public/attachments/75aed3ba6ac543940981ea0221bc1600.png&lt;/img&gt;

75aed3ba6ac543940981ea0221bc1600.png

No, there is no object. It is an array of size zero. Your debugger is telling you that too:

{Object[0]@4076}

That [0] is the size of the array…

Hmm, that is confusing because (as I show in this screenshot) the friends column for that user has at least one object:

http://support.backendless.com/public/attachments/95972ef3380f8229d4e7ff1936df1ed2.png&lt;/img&gt;

95972ef3380f8229d4e7ff1936df1ed2.png

But does that user object has “friends” at the time when you save the Friendship object?

Yes, they already have one friend as shown in the screenshot.

This is why I don’t understand how user.getProperty(“friends”) is coming back with 0 objects.

Could you attach a screenshot showing the Friendship object right before you call save() on it? Preferably with all related properties expanded.

Here is the friendship (self) right before I save it:http://support.backendless.com/public/attachments/78112f5408f9f717586356ba7292163e.png&lt;/img&gt;

This is done from iOS.

I see that friends has 0 values for my users here as well (even though both users here have at least one relation to a User in their friends column):

http://support.backendless.com/public/attachments/307667f2ae86f337d834d84ffde74b12.png&lt;/img&gt;

Well now at least one of my users friends shows that it has objects. But friends for the other user still shows 0 objects for some reason:

http://support.backendless.com/public/attachments/b946cdb483c22f24786b05be854b83c2.png&lt;/img&gt;

It might be worth noting the user for which friends is showing 0 objects is the currentUser.

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:

BackendlessUser[] friends = (BackendlessUser[]) user.getProperty("friends");

It will not get any users for the reason that the users in the original Friendship object have no friends (as you pointed out)

Can’t help you with that. The answer is purely in the app logic…

Ok but is it possible that when you access the current user via:

Backendless.sharedInstance().userService.currentUser

there is something extra you have to do to retrieve its relation properties?

Unless you set current user yourself, that API will return the user object returned by login.

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.

This indeed seems to be related to properties on the currentUser (as opposed to users retrieved otherwise) based on the following:

When I access friends on my currentUser it shows 0 objects for friends.

But when I retrieve that same user via a query (as opposed to getting it via userService.currentUser) it shows that it has 1 friend.

My current workaround for this involves retrieving a BackendlessUser via the currentUser’s objectId:

  class func fetchUser(objectId : String, completed : (user : BackendlessUser?, fault : Fault!) -> Void) {
    let backendless = Backendless.sharedInstance()


    let dataStore = backendless.data.of(BackendlessUser.ofClass())


    dataStore.findID(objectId, response: { (user) -> Void in
      completed(user: user as? BackendlessUser, fault: nil)
      }) { (fault) -> Void in
        print("Server reported an error: \(fault)")
        completed(user: nil, fault: fault)
    }
  }

At least this fetches the user object of the currentUser with all of its related properties attached.