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?
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<BackendlessUser> friends= new ArrayList<BackendlessUser>();
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<BackendlessCollection<BackendlessUser>>() {
@Override
public void handleResponse(BackendlessCollection<BackendlessUser> 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
}
});
}
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<BackendlessUser> friends= new ArrayList<BackendlessUser>();
//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<BackendlessUser>) user.getProperty("friends"))
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>