Relation not removed on save call

Hey guys,
I have two model objects, Party and Recipe. Party contains a List of Recipe objects:

public class Party extends ModelObject {
    private String ownerId;    private String objectId;
    private Long created;
    private Long updated;    private List<Recipe> menuItems = new ArrayList<>();
}
public class Recipe extends ModelObject {
    private String ownerId;    private String objectId;
    private Long created;
    private Long updated;    private String recipeName;
}

And here is my save call:

Backendless.Persistence.of(Party.class).save(party, new AsyncCallback<Party>() {

    @Override
    public void handleResponse(Party party) {
        PartyPlannerLog.v(TAG, "Party Saved");
    }

    @Override
    public void handleFault(BackendlessFault fault) {
        PartyPlannerLog.v(TAG, "Party not Saved");
    }

});

When I make this call, all non-List properties update properly, whether I update a String or boolean, doesn’t matter, it works. But when it comes to the List of Recipe objects in the Party table, I can only add to this list. I can’t remove.

The way I get my Party is as follows:

final protected BackendlessDataQuery dataQuery = new BackendlessDataQuery();dataQuery.setPageSize(100);Backendless.Persistence.of(Party.class).find(dataQuery,
        new AsyncCallback<BackendlessCollection<Party>>() {

            @Override
            public void handleResponse(BackendlessCollection<Party> response) {
                //Get list here
                PartyPlannerLog.v(TAG, "Getting Party success");
            }

            @Override
            public void handleFault(BackendlessFault fault) {
                PartyPlannerLog.v(TAG, "Fail getting Party");
            }
        });

From that list, I get the Party I need, modify the List of Recipe objects, then make the save call, but again, I can only add to the list, not remove items.
Please advise!
Thanks!

Hey, Yusuf

The situation you described is set by default if the object has been retrieved without relations. If you need to change the related objects instead of adding new ones - you have to retrieve an object with all previously saved relations.
To retrieve an object with relations try adding the following:

BackendlessDataQuery query = new BackendlessDataQuery( );
QueryOptions options = new QueryOptions();
options.setRelationsDepth( 1 );
query.setQueryOptions( options );
Backendless.Data.of( YourClass.class ).find(query, ….);

Regards,
Anton

Hi Anton,

I made the change as you suggested but nothing changed. I was always getting back the List of Recipe objects in my List of Party objects already, but I still added the new QueryOptions with setRelationsDepth(1) and I tried (2) as well, but still, removing an object from the list and saving doesn’t work. Please note the snippet of iOS code that my colleague is using that is working just fine. He can remove objects and they save properly:









    func saveParty(party : Party, success : ((Party) -> Void)!, failure : ((Fault!) -> Void)!){

        backendless.persistenceService.of(Party().ofClass()).save(party, response: { (obj :AnyObject!) in

            success(obj as! Party)

            }, error:failure)

    }




    func updateActiveParty(){

        NSNotificationCenter.defaultCenter().postNotificationName(ACTIVE_PARTY_UPDATED_NOTIFICATION, object: nil)

        if let party = Party.activeParty{

            self.saveParty(party, success: { (party : Party) in

                Party.activeParty = party

                }, failure: { (error : Fault!) in

                    print(error)

            })

        }

    }

We tried to reproduce the issue using our own data and everything works fine. Please send us more detailed code sample so we could reproduce the issue.

Regards,
Anton

Are you able to have a Skype call with me to do a screen share and walk through it? Unfortunately due to legal reasons I can’t send you much code.

I understand. As far as backendless functionality works fine we have two possible options:

  1. We don’t need to see your code, just write a similar sample which reproduces the issue, that would be enough for us to see where the problem is.
  2. forward issue details to sales@backendles.com and see our free support policy

Thanks Anton. I have emailed supprt@backendless.com with some code and a detailed explanation of what’s going on.

Hi Yusuf

We’ve created internal ticket BKNDLSS-12951 and assigned it to developers to investigate the issue. We’ll provide you feedback as soon as the problem is solved.

Regards,
Anton

Awesome. Thanks Anton. I hope you guys find something soon, as I need to deliver this project to my client very soon!! Good luck :slight_smile:

Hi Yusuf,

We cannot reproduce this issue. Here is short example which demonstrates that it works as expected.
Don’t forget to set your appId, secrect key and version using Backendless.initApp(…) before calling Backendless.

Regards,

Denys

Hi Denys,

Thanks for looking into this. I ran your example, and it did work. The Guests got removed from the Party. However, if you try to remove a MenuItem instead of a Guest from the party, you will notice that it does not get removed.

Please advise.

Please see my modified code below:

Backendless.UserService.login(email, password, new AsyncCallback<BackendlessUser>() {
  @Override
  public void handleResponse(BackendlessUser response) {

    final Party party = dataStore.findById(partyId);

    System.out.println("BEFORE: " + party);

    party.getMenuItems().remove(0);
    party.getMenuItems().remove(1);
    party.getMenuItems().remove(2);
    dataStore.save(party);

    Party updatedParty = dataStore.findById(partyId);

    System.out.println("\n\nAFTER: " + updatedParty);
  }

  @Override
  public void handleFault(BackendlessFault fault) {
    System.out.println("\n\nERROR: " + fault.toString());
  }
});

Hi Yusuf,

It looks like the root cause of the problem is in how you’re handling equals/hashcode. The bug is not in Backendless SDK or server. Our policy is we do not fix bugs in user’s code. If you would like us to assist you further with this issue, please consider purchasing support.

Regards,

Denys

I can’t believe it!! Thanks for all your help Denys :slight_smile: