Skip to content

Updating Single Object

Updating an object in a data table must be done with a 'data store' object. A reference to a data store can be obtained using the following code:

// obtaining DataStore for the NSDictionary approach
MapDrivenDataStore *dataStore = [Backendless.shared.data ofTable:@"TABLE-NAME"];

// obtaining DataStore for the custom-class approach
DataStoreFactory *dataStore = [Backendless.shared.data of:[YOUR-CLASS class]];
// obtaining DataStore for the NSDictionary approach
let dataStore = Backendless.shared.data.ofTable("TABLE-NAME")

// obtaining DataStore for the custom-class approach
let dataStore = Backendless.shared.data.of(YOUR-CLASS.self)

where

Argument                Description
YOUR-CLASS identifies a table on the server.
"TABLE-NAME" is the name of the table for which to get the data store. All subsequent data store operations will be performed in the specified table.

Important

All Data Service methods in Backendless must be accessed through data stores.

A data store object provides the following APIs for updating data objects in Backendless:

- (void)updateWithEntity:(id _Nonnull)entity responseHandler:^(id _Nonnull)responseHandler errorHandler:^(Fault * _Nonnull)errorHandler;
func update(entity: Any, responseHandler: ((Any) -> Void)!, errorHandler: ((Fault) -> Void)!)
- (void)updateWithEntity:(NSDictionar<NSString *,id> * _Nonnull)entity responseHandler:^(NSDictionar^<NSString *,id> * _Nonnull)responseHandler errorHandler:^(Fault * _Nonnull)errorHandler;
func update(entity: [String : Any], responseHandler: (([String : Any]) -> Void)!, errorHandler: ((Fault) -> Void)!)

where:

Argument                Description
entity object to update in the database.
responseBlock a block (closure) to handle successful result of an asynchronous call.
errorBlock a block (closure) to handle fault result of an asynchronous call.

Return Value

The API returns the updated object as a result of the request.

Example

Consider the following class which will be used in the example. The class will be used to create data objects persisted in Backendless:

@interface Contact : NSObject

@property (strong, nonatomic) NSString *objectId;
@property (strong, nonatomic) NSString *name;
@property (strong, nonatomic) NSString *phone;
@property (strong, nonatomic) NSString *title;
@property (strong, nonatomic) NSNumber *age;

@end
@objcMembers class Contact: NSObject {
    var objectId: String?
    var name: String?
    var phone: String?
    var title: String?
    var age: NSNumber?
}

Important

SWIFT 4 note: The @objcMembers attribute is important. It is required since the Swift object properties are accessed from the Objective-C runtime. For additional information, see the [        Swift 4 programming guide](https://developer.apple.com/library/content/documentation/Swift/Conceptual/BuildingCocoaApps/WritingSwiftClassesWithObjective-CBehavior.html#//apple_ref/doc/uid/TP40014216-CH5-ID86)        .

The following code updates an instance of the Contact class in Backendless:

Contact *existingContact = // some existing contact
existingContact.age = @34;

DataStoreFactory *dataStore = [Backendless.shared.data of:[Contact class]];
[dataStore updateWithEntity:existingContact responseHandler:^(Contact *updatedObject) {
    NSLog(@"Object has been updated: %@", updatedObject);
} errorHandler:^(Fault *fault) {
    NSLog(@"Error: %@", fault.message);
}];
let existingContact: Contact = // some existing contact
existingContact.age = 34

let dataStore = Backendless.shared.data.of(Contact.self)
dataStore.update(entity: existingContact, responseHandler: { updatedObject in
    print("Object has been updated: \(updatedObject)")
}, errorHandler: { fault in
    print("Error: \(fault.message ?? "")")
})

With the dictionary/map-driven approach, objects sent to the backend are represented as plain NSDictionary (in Objective-C) or [String: Any] Dictionary (in Swift):

NSMutableDictionary *existingContact = // some existing contact
existingContact[@"age"] = @34;

MapDrivenDataStore *dataStore = [Backendless.shared.data ofTable:@"Contact"];
[dataStore updateWithEntity:existingContact responseHandler:^(NSDictionary *updatedObject) {
    NSLog(@"Object has been updated: %@", updatedObject);
} errorHandler:^(Fault *fault) {
    NSLog(@"Error: %@", fault.message);
}];
var existingContact: [String: Any] = // some existing contact
existingContact["age"] = 34

let dataStore = Backendless.shared.data.ofTable("Contact")
dataStore.update(entity: existingContact, responseHandler: { updatedObject in
    print("Object has been updated: \(updatedObject)")
}, errorHandler: { fault in
    print("Error: \(fault.message ?? "")")
})

Codeless Reference

data_service_saving_object

where:

Argument                Description
table name Name of the data table where a record must be updated.
object An object with properties that match the names of the table columns, these properties must contain new values for update operation. The objectId property and the corresponding value must be also included in the object.
return result Optional parameter. When this box is checked, the operation returns the updated object.

Returns the updated object.

Consider the following record stored in the employees data table:
data_service_example_update_2

The example below uses the objectId: "18AE9147-8016-412C-8E55-83B3188E153F" to find the record in the data table and update the value in the contactType column from "Personal" to "Work". To update more values in one query, specify the required number properties/column in the object and the new values.

data_service_example_update_1

The result of this operation will look as shown below after the Codeless logic runs:

data_service_example_update_3

Moreover, the operation described above has returned the updated object:

data_service_example_update_4