update a specific data object

Hi,

I’m trying to update a specific data object in backendless, and I can’t seem to get the correct syntax.
how do I join the code for updating:
Backendless.Persistence.save( contact, new AsyncCallback<Contact>() {

with the code for searching a specific data object:
String whereClause = “age = 147”;BackendlessDataQuery dataQuery = new BackendlessDataQuery();dataQuery.setWhereClause( whereClause );BackendlessCollection<Contact> result = Backendless.Persistence.of( Contact.class ).find( dataQuery );

thanks!

do you need to retrieve the object first and then update it?

I want to retrieve the object, set some properties and then update it to the database.

Thanks for the quick reply.

    AsyncCallback&lt;Contact&gt; updateCallback = new AsyncCallback&lt;Contact&gt;()
    {
      @Override
      public void handleResponse( Contact response )
      {
        Log.i( "MY APP", "Contact has been updated" );
      }


      @Override
      public void handleFault( BackendlessFault fault )
      {
        Log.e( "MY APP", "Error " + fault.getMessage() );
      }
    };
    
    AsyncCallback&lt;BackendlessCollection&lt;Contact&gt;> findCallback;
    findCallback = new AsyncCallback&lt;BackendlessCollection&lt;Contact&gt;>()
        {
          @Override
          public void handleResponse( BackendlessCollection&lt;Contact&gt; response )
          {
            // check if you have anything in the response
             if( response.getCurrentPage().size() >> 0 )
             {
               Contact contact = response.getCurrentPage().get( 0 );
               // make changes to the contact object here
               // now save the modified object
               Backendless.Persistence.of( Contact.class ).save( contact, updateCallback );
             }
          }
    
          @Override
          public void handleFault( BackendlessFault fault )
          {
    
          }
        };
    
    String whereClause = "age = 147";
    BackendlessDataQuery dataQuery = new BackendlessDataQuery();
    dataQuery.setWhereClause( whereClause );
    Backendless.Persistence.of( Contact.class ).find( dataQuery, findCallback );

Thanks dear Mark.
I will try it tonight.
Thanks!

Mark, in your code, you get all of the contacts and then perform the whereclause “age = 147” ?
I need to get all of the contacts and only then perform the search for the specific contact ?
I thought that it’s possible to retrieve only the single contact I want and then change it and save.

To load a specific object you can use the findById method if you know objectId:

https://backendless.com/documentation/data/android/data_basic_search.htm

If you do not know object id, use the “find” method, which will return a collection of objects and from the collection you get the one you want.

Mark, I’ve entered this code:

Backendless.Persistence.of( Contact.class).find( new AsyncCallback&lt;BackendlessCollection&lt;Contact&gt;>(){ @Override public void handleResponse( BackendlessCollection&lt;Contact&gt; foundContacts ) {   // all Contact instances have been found } @Override public void handleFault( BackendlessFault fault ) {   // an error has occurred, the error code can be retrieved with fault.getCode() }});

how do I proceed in finding the specific contact I need ?

I don’t know how to “handle” the “foundContacts” list that contains all of the data.

what is the criteria by which you need to find a specific contact?

ok, say I want to find the contact “jim” and update his last name to “kirk”.

what I think should be the code is :


        String whereClause = "name = jim"; // or "name = 'jim'"
        BackendlessDataQuery dataQuery = new BackendlessDataQuery();
        dataQuery.setWhereClause( whereClause );
        BackendlessCollection&lt;Contacts&gt; result = Backendless.Persistence.of(Contacts.class ).find( dataQuery );

        Contacts contacts = result.getCurrentPage().get( 0 );
        contacts.setlastname("kirk");

// save object asynchronously
              Backendless.Persistence.save(contacts, new AsyncCallback&lt;Contacts&gt;() {
                public void handleResponse(Contacts response) {
        // new Contact instance has been saved
                }

               public void handleFault(BackendlessFault fault) {
        // an error has occurred, the error code can be retrieved with fault.getCode()

                   }
             });

what Android Studio thinks is that I’m an idiot and the app crashes in the line: BackendlessCollection<Contacts> result = Backendless.Persistence.of(Contacts.class ).find( dataQuery );

Change the “find” call to be async, or run in a separate thread. Android does not allow synchronous calls on the main UI thread

you mean: "
Backendless.Persistence.of( Contacts ).find( BackendlessDataQuery dataQuery, AsyncCallback<BackendlessCollection<Contacts>> );

"

it’s all red in Android Studio, is it not the correct syntax ?

worked up to:

Backendless.Persistence.of( ExpressActivity.class ).find(dataQuery, AsyncCallback<BackendlessCollection<ExpressActivity>>);

from the AsyncCallback and on its all red errors.please help me get the correct syntax Mark.

Hi Mark,

I’ve succeeded in retrieving the collection from Backendless:

Backendless.Data.of(Contacts.class).find(dataQuery, new AsyncCallback&lt;BackendlessCollection&lt;Contacts&gt;>(){

        @Override
        public void handleResponse( BackendlessCollection&lt;Contacts&gt; foundcontacts )
        {
            // all Contact instances have been found
            contacts = foundcontacts;
        }

but how do I isolate a single Contacts row and get to the point that I can do “contacts.setName” etc ?

how do I work with the collections ?

thanks.

I answered that in another topic you started.

you are the best.

still confused, how to get specific contact out of all

i want to get particular row and then update it

help me out please