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
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<BackendlessUser>) user.getProperty("Following"));
flc.addFollowingusers(ru); //ru is a BackendlessUser
user.setProperty("Following", flc.getFollowingusers());
Backendless.UserService.update(user, new AsyncCallback<BackendlessUser>() {
@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 ?
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.
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<BackendlessUser[]> mylist = new ArrayList<>();
//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<BackendlessUser>() {
@Override
public void handleResponse(BackendlessUser backendlessUser) {
Log.i("following", "added " + ru[0].getEmail()); //works, but the property itself in Backendless is cleared.
}
// 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 :)
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:
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.
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.