adding user to an "one to many" property

Hi,

I have a “one to many” property in my Users table, that I want to add to \ remove from, a single user each time I press a button.

what is the way to do it ? been trying to get the property and add a backendless user to it but I’m sure it’s not the right way to use the relations.

please help, I’m stuck on it for days…
thanks!

Hi Gil,

Please provide some code so that we could better understand what you want to do.
In case you have some one-to-many property in your Users table and want to add objects to it, you can simply do it in your code (using some collection.add(…) method) and then save a parent object. But remember that before doing it you should load the related object using “autoload” or relationDepth or specifying relation manually.

Sergey thanks for the reply. you’re a bit too fast for me :slight_smile:

I’ve tried many ways, the last was to create a class especially for getting \ setting a List<BackendlessUser> list, and adding to it, but I get an error of “wrong schema”:

FollowingListClass flc = new FollowingListClass();

flc.setFollowingusers((ArrayList&lt;BackendlessUser&gt;) user.getProperty("Following"));
flc.addFollowingusers(ru); //ru is a BackendlessUser

user.setProperty("Following", flc.getFollowingusers());
Backendless.UserService.update(user, new AsyncCallback&lt;BackendlessUser&gt;() {
    @Override
    public void handleResponse(BackendlessUser backendlessUser) {
        Log.i("following", "added " + ru.getEmail());

    }

    @Override
    public void handleFault(BackendlessFault backendlessFault) {
        Log.i("following", backendlessFault.getMessage());

    }
});

can you explain about the collection.add and the autoload ?

the class I’ve created:

package com.example.gil.ex;

import com.backendless.BackendlessUser;

import java.util.ArrayList;
import java.util.List;

public class Followinglistclass {

    private BackendlessUser owneruser;
    private ArrayList&lt;BackendlessUser&gt; followingusers;

    public BackendlessUser getOwneruser() {

        return owneruser;
    }

    public void setBackendlessUser( BackendlessUser user) {
        this.owneruser = user;
    }

    public List&lt;BackendlessUser&gt; getFollowingusers()
    {
        return followingusers;
    }

    public void setFollowingusers( ArrayList&lt;BackendlessUser&gt; userstoset )
    {
        this.followingusers = userstoset;
    }
    public void addFollowingusers( BackendlessUser usertoadd )
    {
        this.followingusers.add(usertoadd);
    }


}

The following line of code will not work:

flc.setFollowingusers((ArrayList&lt;BackendlessUser&gt;) user.getProperty("Following"));

Please see the “Retrieve User Property Value” section on the following page:
https://backendless.com/documentation/users/android/users_user_properties.htm
Specifically the paragraph that starts with “There is a special consideration…”

thanks dear Mark.

wasn’t able to get it to work…


   BackendlessUser[] usersObjectArray = (BackendlessUser[]) user.getProperty( "Following" );
   BackendlessUser[] eventsArray = new BackendlessUser[0];

   if( usersObjectArray != null && usersObjectArray.length > 0 ) // this line gets an error
   {
       eventsArray = usersObjectArray;
   }

int ii = eventsArray.length;
   eventsArray[ii+1] = ru; // ru is the user I want to add

   user.setProperty("Following", eventsArray);
   Backendless.UserService.update(user, new AsyncCallback&lt;BackendlessUser&gt;() {
       @Override
       public void handleResponse(BackendlessUser backendlessUser) {
           Log.i("following", "added " + ru.getEmail());

       }

       @Override
       public void handleFault(BackendlessFault backendlessFault) {
           Log.i("following", backendlessFault.getMessage());

       }
   });

You didn’t follow the suggestions from the documentation I referenced.

Hi Mark,
I wrote the same code as in the reference, only in my app related variable names… :
BackendlessUser[] usersObjectArray = (BackendlessUser[]) user.getProperty( “Following” );
BackendlessUser[] eventsArray = new BackendlessUser[0];
if( usersObjectArray != null && usersObjectArray.length > 0 ) // this line gets an error
{
eventsArray = usersObjectArray;
}

I’d really appreciate your help in adding a user to a one to many users list.
Thank you.

No, you didn’t write the same code as in the reference. In the reference the code initially is cast to Object[]:

Object[] eventsObjectArray = (Object[]) user.getProperty( "events" );

While your code casts the result immediately to BackendlessUser[], which is exactly the think the documentation warns you NOT TO DO:

Before application casts the array object to an array of a specific type, it must check if the size of the array is greater than zero

Hi dear Mark,

thank you for pointing out the objects array issue.

let’s try to clean this topic a little so that everyone who makes a social app with Backendless could read a good way of adding \ removing other users.

my code -almost- works, I try to get the “following” (one to many Users) of the current user and and add the user he wants to add to his following list.

the problem is that I get a blank list. probably something obvious that I’m missing with the arrays:

Object[] usersObjectArray = (Object[]) user.getProperty( "Following" );
ArrayList&lt;BackendlessUser[]&gt; mylist = new ArrayList&lt;&gt;();

//if( usersObjectArray != null && usersObjectArray.length > 0 ) {//commented out the 'if' because the user will have an empty followers list at start.

    BackendlessUser[] eventsArray = new BackendlessUser[usersObjectArray.length+1]; //reserving a place for the new user

    mylist.add(eventsArray); //adding the full following list
    mylist.add(ru); // adding the new user

//}

user.setProperty("Following", mylist);
Backendless.UserService.update(user, new AsyncCallback&lt;BackendlessUser&gt;() {
    @Override
    public void handleResponse(BackendlessUser backendlessUser) {
        Log.i("following", "added " + ru[0].getEmail()); //works, but the property itself in Backendless is cleared.
    }

I do not understand the logic here:

// you create an empty array
BackendlessUser[] eventsArray = new BackendlessUser[usersObjectArray.length+1]; 
// you add that empty array to "mylist"
mylist.add(eventsArray); 
// then you add the "ru" object to the list
mylist.add(ru); 

what is the point of creating an empty array and then adding to mylist?

it’s a null logic probably… sorry I’ve been stuck at this single point for over a week now and I’m really frustrated over it.

even when I use the exact code from the documentary, I still don’t understand how to add the new user to the array before I update the user property in Backendless.


        Object[] eventsObjectArray = (Object[]) user.getProperty( "following" );
        BackendlessUser[] followingusers;

     // if( eventsObjectArray != null && eventsObjectArray.length > 0 ) //commented out because the user has 0 following at start
        followingusers = (BackendlessUser[]) eventsObjectArray;

somehow.add.thenewusertothe.otheruserswhoarefollowinglist.errrrrrr; // not actually Java :)

ArrayList<BackendlessUser> usersList = Arrays.asList( followingusers );

usersList.add( userObject )

Mark, first of all, I really appreciate your help and patience. more than I think that Backendless is the best backend service (tried all the leading ones, including game-oriented backends), I think that the support you guys (and Kate) give us is great. thanks for your patience in my first Backendless project.

couldn’t get it to work, for the following (ha ha) reasons:

   Object[] eventsObjectArray = (Object[]) user.getProperty( "following" );
   BackendlessUser[] followingusers;

// if( eventsObjectArray != null && eventsObjectArray.length > 0 ) // commented out
   followingusers = (BackendlessUser[]) eventsObjectArray;
   Log.i("follow", String.valueOf((followingusers.length)));
   ArrayList&lt;BackendlessUser&gt; usersList = Arrays.asList( followingusers ); // doesn't compile, required ArrayList, found list
 // List&lt;BackendlessUser&gt; usersList = Arrays.asList(followingusers); // crashes
 //  ArrayList&lt;BackendlessUser&gt; usersList = (ArrayList&lt;BackendlessUser&gt;) Arrays.asList( followingusers ); // crashes
 //  List&lt;BackendlessUser&gt; usersList = (ArrayList&lt;BackendlessUser&gt;) Arrays.asList( followingusers ); // crashes

   usersList.add( ru );

List<BackendlessUser> usersList = Arrays.asList( followingusers );

Dear Mark,
I got an “UnsupportedOperationException” in the line : usersList.add(ru);my solution was to use a LinkedList. it works now:

        Object[] eventsObjectArray = (Object[]) user.getProperty( "Following" );
        BackendlessUser[] followingusers;
          followingusers = (BackendlessUser[]) eventsObjectArray;
          List&lt;BackendlessUser&gt; usersList = Arrays.asList( followingusers );
          List&lt;BackendlessUser&gt; newList = new LinkedList&lt;BackendlessUser&gt;(Arrays.asList(followingusers));
        newList.add(ru);

guessing that removing a user will be the same, only with "newList.remove( ru );"thanks a lot for you time and patience. I really appreciate it.now I can finally finish my app.

of course my guess was wrong. can’t remove users from the list :slight_smile:

tried both newList.remove(newList.indexOf(ru)); (as an int) and newList.remove(ru); (as an object).

Gil, as much as I would love to help, these questions/problems are related to Java usage and not Backendless. Please try posting to stackoverflow.com or other resources. It is important that we maintain focus here strictly on our API/backend because otherwise we will be stretching very thin.

Hope you understand.

Regards,
Mark

yes Mark of course I understand. I appriciate all the help.