Unable to update relation

I have a class called Recipe that contains a 1:M relation to Users. I am trying to remove a user from a relation. Steps I have taken.

    Retrieve Recipe object. Retrieve relations including Users. Remove current user from relation (I am able to verify it is removed) Save Recipe object
I receive no errors. The callback is successful and the object returned in the callback shows the updated relation information. I have also tried changing other fields which do get saved. However the relation is never changed whether I try to add or remove a User. I have tried using 3.0.15.1 and 3.0.17.1. The java classes I am using are the classes created from code gen so I wont post those. Here is the relevant code to removing and saving the recipe object.
Iterator<BackendlessUser> userIterator = mRecipeObject.getUsers().iterator();
                        String currrentUserId = Backendless.UserService.CurrentUser().getObjectId();
                        while (userIterator.hasNext()) {
                            String userId = userIterator.next().getObjectId();
                            if (userId.equals(currrentUserId)) {
                                userIterator.remove();
                                break;
                            }
                        }
Have verified at this point that the Users relation is updated.
Backendless.Data.of(Recipe.class).save(mRecipeObject, new AsyncCallback<Recipe>() {
                            @Override
                            public void handleResponse(Recipe response) {
                            Skipped for brevity
                            repsonse object has updated Users relation as well.
                            }
                            @Override
                            public void handleFault(BackendlessFault fault) {
                             Skipped for brevity.
                            }
                        });

Bob,

The description and the code look good. We will try duplicating the problem and report back.

Regards,
Mark

Quick question though: the code you posted removes a user and in the code you have the following comment:

// response object has updated Users relation as well.

Does it mean the update worked when you deleted a related user? Or perhaps you meant that the response object looks correct, but the relation is not updated when you checked in Backendless console? Please clarify.

Mark

The response object is correct but nothing changes in the console. After posting, I tried to make changes to other relations on the recipe object I can not update any relation propertes. Any non relational data is correctly updated though.

Hi, Bob!

Did you set the “autoload” checkbox for your Users relation in Recipe table (in Backendless console)?
Here’s the sample of code that removes the relation.
Please try if it works for you.
If you still encounter an error please send us your application id at support@backendless.com
Recipe recipe = Backendless.Data.of( Recipe.class ).findFirst();
recipe.getUsers().remove( 0 );

Backendless.Data.of( Recipe.class ).save( recipe, new AsyncCallback<Recipe>()
{
@Override
public void handleResponse( Recipe response )
{
System.out.println( response );
}

@Override
public void handleFault( BackendlessFault fault )
{
System.out.println( fault );
}
} );

Hi, Bob!

Do you still encounter this error?
I’ve just checked your exact code and it works fine.

I just tried again. Same result. Any other properties changed get properly saved. Relations are unchanged. I’ll keep digging and see if there is something else I am missing. I am able to create objects with new relations but otherwise cannot modify those relations.

Update: I am able to add relations to an object. However I still can not remove relations. I tried using remove() to modify the returned List, I tried creating a new blank list and saving that to the object, and I tried setting the List to null and saving that. All three options fail to update the relation. I have also tried it with 2 other relations on the object. I can only add items to the 3 but not remove.

Bob,

Any chance you could zip up a project which reproduces the error and upload to either dropbox or google drive? If you can, please send a link to the project to support@backendless.com. Being able to reproduce the error will be a tremendous help in fixing the problem. We had two engineers try the exactly the same use-case and it worked in both cases.

Regards,
Mark

While putting together a test project I did find that if autoload is checked then I can delete the relation. Is this necessary? There are times I don’t wish to have the relations autoload. I can still upload the demo project if needed.

I have solved the issue. Removing relations does not work with two step relation retrieval. Either autoload or single step retrieval must be used.

Hi, Bob.

Please read our documentation on retrieving relations (you can use relationsDepth parameter to define the level of relations you need to upload) - https://backendless.com/documentation/data/android/data_relations_retrieve.htm

I see that now. The way I was originally handling the relations was the original object was loaded without relations (to avoid the page size limit) once a user selected that object it was passed to another fragment which then loaded the relations into the existing object. I am not sure why that method didn’t work, I have modified my code to just requery the object instead of passing it as a parameter.

Hi Bob, I’m having the same issue you were, but not quite sure how you solved it. What do you mean by “Removing relations does not work with two step relation retrieval. Either autoload or single step retrieval must be used.” ?

You either have to use the auto load option in the Backendless console or you can retrieve the object with relations at the same time. Originally I was loading Object A and passing it to a new fragment which loaded the relations and attached them to Object A this was breaking The ability to update any relations.

So I have autoload set on all my relations. What I do is when my app loads, I load a List of Party objects. I then remove one of the Recipe objects from the List of Recipe’s in the Party, and then save the Party object back to the server. In my handleResponse callback, I get the party back with the correct number of Recipe objects in the List, but the server remains unchanged. Any ideas?