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:

String friends;

String objectId="5491F128-60D2-B9FA-FFF5-8DC31050F600";



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!

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:
http://support.backendless.com/public/attachments/9e611986e9379be80927a8e6f4767c89.jpg</img>

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

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!

   private void findFriends() {
        BackendlessUser currentUser = Backendless.UserService.CurrentUser();
        final List&lt;BackendlessUser&gt; friends= new ArrayList&lt;BackendlessUser&gt;();


        String whereClause = "email='v@v.com'";


        BackendlessDataQuery query = new BackendlessDataQuery();
        QueryOptions queryOptions = new QueryOptions();
        query.setWhereClause(whereClause);
        queryOptions.addRelated( "friends" );
        queryOptions.addRelated( "friends.RELATION-OF-RELATION" );
        query.setQueryOptions( queryOptions );




         Backendless.Data.of(BackendlessUser.class).find(query, new AsyncCallback&lt;BackendlessCollection&lt;BackendlessUser&gt;>() {
           @Override
           public void handleResponse(BackendlessCollection&lt;BackendlessUser&gt; collection) {
               if (collection.getCurrentPage().isEmpty()) {
                   //no friends


               } else {
                   //create list of objects
                   int numberOfFriends = collection.getCurrentPage().size();
                   for (int i = 0; i < numberOfFriends; i++) {
                       friends.add(collection.getCurrentPage().get(i));
                   }


               }




           }


           @Override
           public void handleFault(BackendlessFault backendlessFault) {
//error
           }
       });


    }

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

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.

List&lt;BackendlessUser&gt; friends= new ArrayList&lt;BackendlessUser&gt;();
//this is the collection returned from the query. It returns only the current user with the friends property
BackendlessUser user = collection.getCurrentPage().get(0);
friends = (List&lt;BackendlessUser&gt;) user.getProperty("friends"))

Thanks for your help!
Victor

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?

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>

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

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

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

Regards,
Mark

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

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