Skip to content

Updating Single Object

Blocking Method

Dictionary<string, object> result; 

result = Backendless.Data.Of( "TABLE-NAME" ).Save( Dictionary<string, object> );
public E Backendless.Data.Of<E>().Save( E entity );

Non-Blocking API

void Backendless.Data.Of( "TABLE-NAME" ).Save( Dictionary<string, object> entity, 
                                               AsyncCallback<Dictionary<string, object>> responder )
public void Backendless.Data.Of<E>().Save( E entity, AsyncCallback<E> responder )

where:

Argument                Description
TABLE-NAME Name of the table where the object represented by System.Collections.Generic.Dictionary will be updated.
E A .NET class of the data object to update.
entity .NET object to update, must be of type E or System.Collections.Generic.Dictionary (depending on the method used).
responder a responder object which will receive a callback when the method successfully updates the object or if an error occurs. Applies  to the non-blocking method only.

Return Value

The blocking method returns the updated object. The non-blocking call receives the return value through a callback executed on the AsyncCallback object.

Example

Blocking API

static void UpdateContact()
{
  // create new contact object first. Then we will update it.
  Dictionary<string, object> contact = new Dictionary<string, object>();
  contact.Add( "Name", "Jack Daniels" );
  contact.Add( "Age", 147 );
  contact.Add( "Phone", "777-777-777" );
  contact.Add( "Title", "Favorites" );

  Dictionary<string, object> savedContact = Backendless.Persistence.Of( "Contact" ).Save( contact );

  // now update the saved object
  savedContact[ "Title" ] = "Most favorite";
  Backendless.Persistence.Of( "Contact" ).Save( savedContact );
}

Non-blocking API

static void UpdateContactAsync()
{
  // create new contact object first. Then we will update it.
  Dictionary<string, object> contact = new Dictionary<string, object>();
  contact.Add( "Name", "Jack Daniels" );
  contact.Add( "Age", 147 );
  contact.Add( "Phone", "777-777-777" );
  contact.Add( "Title", "Favorites" );

  AsyncCallback<Dictionary<string, object>> updateObjectCallback;
  updateObjectCallback = new AsyncCallback<Dictionary<string, object>>(
    savedContact =>
    {
      System.Console.WriteLine( "object has been updated" );
    },
    error =>
    {
    }
  );

  AsyncCallback<Dictionary<string, object>> saveObjectCallback;
  saveObjectCallback = new AsyncCallback<Dictionary<string, object>>(
    savedContact =>
    {
      System.Console.WriteLine( "object has been created, now make an API call to update it" );
      // now update the saved object
      savedContact[ "Title" ] = "Most favorite";
      Backendless.Persistence.Of( "Contact" ).Save( savedContact, updateObjectCallback );
    },
    error =>
    {
    }
  );

  Backendless.Persistence.Of( "Contact" ).Save( contact, saveObjectCallback );
}
Consider the following class:
public class Contact
  {
    //use the Weborb.Service namespace to import the annotation
    [SetClientClassMemberName( "objectId" )]   
    public String ObjectId { get;set; }
    public String Name { get; set; }
    public int Age { get; set; }
    public String Phone { get; set; }
    public String Title { get; set; }
  }
The following code saves a new instance of the Contact class and subsequently updates it:

Blocking API

static void UpdateContactUsingClass()
{
  // create new contact object first. Then we will update it.
  Contact contact = new Contact();
  contact.Name = "Jack Daniels";
  contact.Age = 147;
  contact.Phone = "777-777-777";
  contact.Title = "Favorites";

  Contact savedContact = Backendless.Persistence.Of<Contact>().Save( contact );

  // now update the saved object
  savedContact.Title = "Most favorite";
  Backendless.Persistence.Of<Contact>().Save( savedContact );
}

Non-blocking API

static void UpdateContactAsyncUsingClass()
{
  // create new contact object first. Then we will update it.
  Contact contact = new Contact();
  contact.Name = "Jack Daniels";
  contact.Age = 147;
  contact.Phone = "777-777-777";
  contact.Title = "Favorites";

  AsyncCallback<Contact> updateObjectCallback = new AsyncCallback<Contact>(
    savedContact =>
    {
      System.Console.WriteLine( "object has been updated" );
    },
    error =>
    {
    }
  );

  AsyncCallback<Contact> saveObjectCallback = new AsyncCallback<Contact>(
    savedContact =>
    {
      System.Console.WriteLine( "object has been created, now make an API call to update it" );
      // now update the saved object
      savedContact.Title = "Most favorite";
      Backendless.Persistence.Of<Contact>().Save( savedContact, updateObjectCallback );
    },
    error =>
    {
    }
  );

  Backendless.Persistence.Of<Contact>().Save( contact, saveObjectCallback );
}