Backendless Support
 
Answered

friend relations Android

Hi,

I need to

create relationships between users in my app. The idea is that users must be

able to add each other as ‘friends’ and send messages to one or more of their ‘friends’.

If I understand

correctly the suggested way to do that is to add a column in the Users table

that will store the objectIDs of the ‘friends’ separated by commas, correct?

How can I

then retrieve the ‘friend’ objects in one go instead of running a query for

each individual objectId?

To retrieve

the objectIds of the frinds I use:

  1. String friends;
  2. String objectId="5491F128-60D2-B9FA-FFF5-8DC31050F600";
  3. Backendless.UserService.findById(objectId,

    new AsyncCallback<BackendlessUser>() {

    @Override

    public void

    handleResponse(BackendlessUser backendlessUser) {

    friends = (String)

    backendlessUser.getProperty(“friends”);

    }

    @Override

    public void

    handleFault(BackendlessFault backendlessFault) {

    //handle error

    }

    });

Is there a

way to use the String friends to retrieve all backendlessUser objects with one

query instead of splitting the friends string and running a query for each

individual ‘friend’ objectId?

Thanks!

Leave a Comment

Comments (8)

photo
1

There is a simpler way to do it. You need to establish a relation between the Users table and the same Users table. To do this, go to your app in console, select Users and in the User Properties click the "Add Relationship" button. Take a look at this screenshot:

9e611986e9379be80927a8e6f4767c89

Create a relationship as shown in the image above. I have it as "one to many", but you could easily chose one-to-one as well.

Once that is done, you can query and get all the friends in one go using any of the approaches for loading relations (the doc is for Android, so pick your client type):

http://backendless.com/documentation/data/android/data_relations_retrieve.htm

Hope this helps.

Regards,

Mark

photo
1

Thanks Mark, this works!

I added a couple of users manually together with relations between them to test this but I can't get the query for loading relations working properly.

The problem is that if I include the where clause in the query I only get the current user as a result. If I don't include a where clause in the query I get all registered users. What I am trying to achieve is write a query that will return only the 'friends' of the current user.

My code is below. Thank you for the help!

  1. private void findFriends() {
  2. BackendlessUser currentUser = Backendless.UserService.CurrentUser();
  3. final List<BackendlessUser> friends= new ArrayList<BackendlessUser>();
  4. String whereClause = "email='v@v.com'";
  5. BackendlessDataQuery query = new BackendlessDataQuery();
  6. QueryOptions queryOptions = new QueryOptions();
  7. query.setWhereClause(whereClause);
  8. queryOptions.addRelated( "friends" );
  9. queryOptions.addRelated( "friends.RELATION-OF-RELATION" );
  10. query.setQueryOptions( queryOptions );
  11. Backendless.Data.of(BackendlessUser.class).find(query, new AsyncCallback<BackendlessCollection<BackendlessUser>>() {
  12. @Override
  13. public void handleResponse(BackendlessCollection<BackendlessUser> collection) {
  14. if (collection.getCurrentPage().isEmpty()) {
  15. //no friends
  16. } else {
  17. //create list of objects
  18. int numberOfFriends = collection.getCurrentPage().size();
  19. for (int i = 0; i < numberOfFriends; i++) {
  20. friends.add(collection.getCurrentPage().get(i));
  21. }
  22. }
  23. }
  24. @Override
  25. public void handleFault(BackendlessFault backendlessFault) {
  26. //error
  27. }
  28. });
  29. }

photo
1

With the where clause, when you get a user in the result, does that user object contain a value for the "friends" property?

Also, make sure you're using the latest Android SDK.

Regards,

Mark

photo
1

Yes, actually it does return the friends property. What is the appropriate syntax to put the friend objects into a List of Backendless users?

I tried the following but it does not work.

  1. List<BackendlessUser> friends= new ArrayList<BackendlessUser>();
  2. //this is the collection returned from the query. It returns only the current user with the friends property
  3. BackendlessUser user = collection.getCurrentPage().get(0);
  4. friends = (List<BackendlessUser>) user.getProperty("friends"))

Thanks for your help!

Victor

photo
1

Can you elaborate on " it does not work." ?

For example, what error do you get? Is there a stack trace? Have you checked in the debugger what value you get back from the server?

photo
1

Mark,

Thanks for the tips.

The problem was that it returns a HashMap<String,Object>[] value and not an List<BackendlessUser> value as I was expecting.

I did a search in the forum and the suggested solution was to map the table to the class.

I tried tried doing with the statement below but I the result of the query is still HashMap instead of a List<BackendlessUsers>

  1. Backendless.Data.mapTableToClass("Users", UserService.class);

photo
1

Victor, you were very close. Here's the correct line of code:

  1. Backendless.Data.mapTableToClass( "Users", BackendlessUser.class );

Regards,

Mark

photo
1

Thanks Mark,

It now works by executing the code below in the handleResponse block of the query.

  1. BackendlessUser user = collection.getCurrentPage().get(0);
  2. BackendlessUser[] friends = (BackendlessUser[]) user.getProperty("friends");

photo