Backendless Support
 
Waiting for user's response

I can save relation BUT can't delete relation ?

hi guys,

in my app a user can join chat group with multiple users ... so in my app data there is (Groups) table

and each group have a relation with users table via (Members) schema

the problem is ..... i can add(save) relations (users) to the group via this code :

  1. Backendless.Persistence.of( Groups.class ).findById( group.objectId,

    new AsyncCallback<Groups>() {

    @Override

    public void handleResponse(final Groups group ){

    // Get the user

    Backendless.UserService.findById(theMember.getUserObjectId(),

    new AsyncCallback<BackendlessUser>() {

    @Override

    public void handleResponse( BackendlessUser user ) {

    group.getMembers().add(user)

    //Update group

    Backendless.Persistence.of( Groups.class ).save( group, new AsyncCallback<Groups>() { @Override

    public void handleResponse( Groups response ) {

    Adapter.notifyDataSetChanged();

    }

    @Override

    public void handleFault( BackendlessFault fault ) {

    }

    });

    }

    @Override

    public void handleFault( BackendlessFault fault ) {

    }

    });

    }

    @Override

    public void handleFault( BackendlessFault fault ){

    }

    });

and its work fine ... But when am trying to delete a user from a group (relation) via this code:

  1. Backendless.Persistence.of( Groups.class ).findById( group.objectId,

    new AsyncCallback<Groups>() {

    @Override

    public void handleResponse(final Groups group ){

    members = new ArrayList<>(group.getMembers());

    members.remove(position); // as the row position that user pressed

    group.setMembers(members); //Update group

    Backendless.Persistence.of( Groups.class ).save( group, new AsyncCallback<Groups>() { @Override

    public void handleResponse( Groups response ) {

    Adapter.notifyDataSetChanged();

    }

    @Override

    public void handleFault( BackendlessFault fault ) {

    }

    }); }

    @Override

    public void handleFault( BackendlessFault fault ) {

    }

    });

    }

    @Override

    public void handleFault( BackendlessFault fault ){

    }

    });

It's not working .... So any ideas ?

Leave a Comment

Comments (17)

photo
1

if because you should NOT copy members

  1. members = new ArrayList<>(group.getMembers());

    members.remove(position); // as the row position that user pressed

but delete from collection like this

  1. group.getMembers().remove(position);

    // than save

photo
1

thanks for responding ... but i also tried this

  1. group.getMembers().remove(position);

    // than save

Not working , the relation still there!!!!!

photo
photo
1

thanks for responding ... but i also tried this

  1. group.getMembers().remove(position);

    // than save

Not working , the relation still there!!!!!

photo
1

have you call Backendless.Persistence.of( Groups.class ).save( group, new AsyncCallback<Groups>()...?

photo
1

Yes.... this is my code

  1. Backendless.Persistence.of( Groups.class ).save( group, new AsyncCallback<Groups>() {

    @Override

    public void handleResponse( Groups response )

    {

    System.out.println("Success");

    Adapter.notifyDataSetChanged();

    }

    @Override

    public void handleFault( BackendlessFault fault )

    {

    System.out.println("Error Message: " + fault.getMessage());

    System.out.println("Error Code: " + fault.getCode());

    System.out.println("Error Detail: " + fault.getDetail());

    }

    } );

photo
photo
1

here it is my example

  1. final IDataStore<Goups> goupsIDataStore = Backendless.Data.of( Goups.class );

    System.out.println( "Before remove: " + goupsIDataStore.find().getCurrentPage().get( 0 ).getMembers().size());

    goupsIDataStore.findFirst( new AsyncCallback<Goups>()

    {

    @Override

    public void handleResponse( Goups response )

    {

    response.getMembers().remove( 0 );

    goupsIDataStore.save( response, new AsyncCallback<Goups>()

    {

    @Override

    public void handleResponse( Goups response )

    {

    System.out.println( "After remove: " + goupsIDataStore.find().getCurrentPage().get( 0 ).getMembers().size());

    }

    @Override

    public void handleFault( BackendlessFault fault )

    {

    }

    } );

    }

    @Override

    public void handleFault( BackendlessFault fault )

    {

    }

    }

    );

and result is

  1. /Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/bin/java -Didea.launcher.port=7533 "-Didea.launcher.bin.path=/Applications/IntelliJ IDEA 15.app/Contents/bin" -Dfile.encoding=UTF-8 -classpath "/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/lib/ant-javafx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/lib/dt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/lib/javafx-mx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/lib/jconsole.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/lib/sa-jdi.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/lib/tools.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/jre/lib/charsets.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/jre/lib/deploy.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/jre/lib/javaws.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/jre/lib/jce.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/jre/lib/jfr.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/jre/lib/jfxswt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/jre/lib/jsse.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/jre/lib/management-agent.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/jre/lib/plugin.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/jre/lib/resources.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/jre/lib/rt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/jre/lib/ext/cldrdata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/jre/lib/ext/dnsns.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/jre/lib/ext/jfxrt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/jre/lib/ext/localedata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/jre/lib/ext/nashorn.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/jre/lib/ext/sunec.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/jre/lib/ext/sunjce_provider.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/jre/lib/ext/sunpkcs11.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/jre/lib/ext/zipfs.jar:/Users/ksv/dev/tmp/somereltest/build/classes/main:/Users/ksv/dev/tmp/somereltest/build/resources/main:/Users/ksv/.gradle/caches/modules-2/files-2.1/com.backendless/backendless/3.0.20.1/282a3924b627255f8cde19093702f39e929f9875/backendless-3.0.20.1.jar:/Applications/IntelliJ IDEA 15.app/Contents/lib/idea_rt.jar" com.intellij.rt.execution.application.AppMain SomeMain
  2. Before remove: 3
  3. After remove: 2

so it works

photo
1

I know its working and give me the right response....look i copied your example :

and here my result on the logs :

  1. I/System.out: Before deleting 4
  2. I/System.out: NAME S
  3. I/System.out: NAME K
  4. I/System.out: NAME M
  5. I/System.out: after 3

BUT the problem is nothing changed on the console , the user is still there and when i refresh the activity the results returned to 4 ?????

photo
1

Hey Nasser,

if you copied the code, how come the "System.out.println" is different? That is, Sergey's code uses the "Before remove" and "After remove" in the println messages..

Mark

photo
1

hey Mark,

i changed a little things.....but the main code is there... her is my code:

  1. final IDataStore<Groups> groupIDataStore = Backendless.Data.of( Groups.class );

    System.out.println("Before deleting " + group.getMembers().size()); qattahIDataStore.findById( group.getObjectId(),

    new AsyncCallback<Groups>() {

    @Override

    public void handleResponse(final Groups group ) {

    group.getMembers().remove(1);

    //Update group

    groupIDataStore.save( group, new AsyncCallback<SubQattahs>() {

    @Override

    public void handleResponse( Groups response ) {

    for (BackendlessUser user: response.getMembers()) {

    System.out.println("NEW " + user.getProperty("name"));

    }

    System.out.println("after "+group.getMembers().size());

    Adapter.notifyDataSetChanged();

    }

    @Override

    public void handleFault( BackendlessFault fault )

    {

    System.out.println("Error Message: " + fault.getMessage());

    System.out.println("Error Code: " + fault.getCode());

    System.out.println("Error Detail: " + fault.getDetail());

    }

    } );

    }

    @Override

    public void handleFault( BackendlessFault fault )

    {

    // an error has occurred, the error code can be retrieved with fault.getCode()

    }

    });

i have 4 users in group.getMembers()

S

N

K

M

so the result

  1. I/System.out: Before deleting 4
  2. I/System.out: NAME S
  3. I/System.out: NAME K
  4. I/System.out: NAME M
  5. I/System.out: after 3

BUT Nothing changed on the console !?

photo
1

Nasser, we verified that the API works. The free support does not include us debugging your code, which is where I believe the problem is. If you would like us to look into your code and find the problem, please purchase either support plan or "single incident support" from the Marketplace.

Regards,

Mark

photo
1

thanks Mark,

photo
photo
1

if you delete it from collection it does not mean that you delete the member, it mean that you just break relation between Groups and Members. To delete member you should call remove method Backendless.Data.of( Members.class ).remove( )

photo
1

yes, I just want to break the relation between the group and the user !?

NOT deleting the USER from the data!

photo
photo
1

OK..I have a question about deleting relations,

Consider the following (Backendless) example :

2a4b61f9c07a28c6d381bb0d270dbbac

let say , in my case, my app data have multiple phonebooks !?

and each phonebook have multiple contacts!

and each contact have multiple addresses!

So, if i want to delete a relation between a contact AND one if it's addresses!

* Is it enough to retrieve the exact contact by it's objectId and then remove the address from contact.getAddresses() list ?

OR

*Do I have to retrieve the phonebook and then find the contact from phonebook.getContacts() list and then get the address from contact.getAddresses() list , AND last, remove it?

photo
1

it is enough to retrieve the exact contact by it's objectId and then remove the address from contact.getAddresses() list and then save contact

photo
1

Thank you very much Sergey

Any suggestions about my case up there !?

photo
1

I have produced example of my code, is this the same case as yours?