How to remove user from relation?

Hello guys,
I have successfully created a relation between Users and my Feed class. Now I have to remove current user from that relation on a centain button click. How can I do that?
I’m saving user to that relation with the following code:

BackendlessUser currentUser = Backendless.UserService.CurrentUser();
List<BackendlessUser> list = new ArrayList<>();
list.add(currentUser);
feeds.setUsersThatLike(list);


Than I save the Feeds class.
Here is the setUsersThatLike function in Feeds.class:

public class Feeds {
private List<BackendlessUser> usersThatLike;
public List<BackendlessUser> getUsersThatLike() {
return usersThatLike;
}
public void setUsersThatLike(List<BackendlessUser> usersThatLike) {
this.usersThatLike = usersThatLike;
}
}

I tried the following code so far:

BackendlessUser currentUser = Backendless.UserService.CurrentUser();



List<BackendlessUser> list = feeds.getUsersThatLike();



list.remove(currentUser);



feeds.setUsersThatLike(list);

Than I save it.
But this removes all the users from the relation list.
I tried to manually add a user to that relation and than added myself from my Android device clicking a button. It saved me to relation. Than I click another button with the code above to remove myself but this removes me and the second user that I added from backendless console…

Hi Asama,

The doc describing removal of related objects is here:
https://backendless.com/documentation/data/android/data_relations_delete.htm

I have a few questions about the following code:

BackendlessUser currentUser = Backendless.UserService.CurrentUser();
List<BackendlessUser> list = feeds.getUsersThatLike();
list.remove(currentUser);
feeds.setUsersThatLike(list);
    How does the code retrieve the related user objects for the "feeds" object? Have you verified in a debugger that the "currentUser" object is indeed removed from "list"?
Mark

Hi Mark,

1- Here’s how I get userThatLike data:
In my Feeds.class:

public List<BackendlessUser> getUsersThatLike() {
    return usersThatLike;
}

2- Sorry but I don’t know how to do that…

I assume for (1) you enabled “autoload” in Backendless console.

To solve this problem you need to figure out how to debug your own code. There are plenty of resources on the internet describing it.

I know how to debug the code but I don’t know how to check if the currentUser object is being removed.

Just make sure the “list” object does not have the user you’re removing from the list.

I have tried to print the list of users that are in the “usersThatLike” relation and the current user is still in there.

Please provide a snippet of code to remove the current user from that list.
Thanks

Removing an object from a list is a standard method in Java.

I tried:
feeds.getUsersThatLike().remove(Backendless.UserService.CurrentUser);
And than save the feeds:
Backendless.Data.of(Feeds.class).save(feeds, new AsyncCallback…
And in handleResponse I print the list as string in the log window and the user hasn’t been removed.

You need to verify that after this line of code:

feeds.getUsersThatLike().remove(Backendless.UserService.CurrentUser);

the user is indeed removed from feeds.getUsersThatLike() (because the method might be removing a copy of collection containing users)

Well if I try: feed.getUsersThatLike().remove(0); than it removes the first user that like a certain item.
I really don’t know how to find a solution for this. I also tried to follow the docs android api guide to remove an item from relation and also there it used: savedPhone.getContacts().remove(0); but isn’t there a way to remove the current user who is clicking on that button?

It seems like you’re having problem at the Java level, not Backendless. This is not covered by free support. If you’d like us to help you solve the problem and correct your code, a single support incident costs $100. Please contact sales@backendless.com and we can assist you.