Skip to content

Updating Multiple Objects

This API updates multiple objects in a data table with a single request. Consider the following example, it demonstrates an API call which updates all objects in the Person data table where the value of the property age is greater than 20. As a result of the request, all updated objects will have the contactType property set to "personal":

MapDrivenDataStore *dataStore = [Backendless.shared.data ofTable:@"Person"];
NSDictionary *changes = @{@"contactType": @"personal"};
[dataStore updateBulkWithWhereClause:@"age>20" changes:changes responseHandler:^(NSNumber *updated) {
    NSLog(@"%@ objects have been updated", updated);
} errorHandler:^(Fault *fault) {
    NSLog(@"Error: %@", fault.message);
}];
let dataStore = Backendless.shared.data.ofTable("Person")
let changes = ["contactType": "personal"]
dataStore.updateBulk(whereClause: "age>20", changes: changes, responseHandler: { updated in
    print("\(updated) objects have been updated")
}, errorHandler: { fault in
    print("Error: \(fault.message ?? "")")
})

Available methods

- (void)updateBulkWithWhereClause:(NSString * _Nullable)whereClause changes:(NSDictionary<NSString *,id> * _Nonnull)changes responseHandler:^(NSNumber * _Nonnull)responseHandler errorHandler:^(Fault * _Nonnull)errorHandler;
func updateBulk(whereClause: String?, changes: [String : Any], responseHandler: ((NSNumber) -> Void)!, errorHandler: ((Fault) -> Void)!)
- (void)updateBulkWithWhereClause:(NSString * _Nullable)whereClause changes:(NSDictionary<NSString *,id> * _Nonnull)changes responseHandler:^(NSNumber * _Nonnull)responseHandler errorHandler:^(Fault * _Nonnull)errorHandler;
func updateBulk(whereClause: String?, changes: [String : Any], responseHandler: ((NSNumber) -> Void)!, errorHandler: ((Fault) -> Void)!)

where:

Argument                Description
whereClause a condition for selecting objects in the data table which will be updated.
changes a dictionary object containing the changes which will be applied to all objects in the data table which match the condition expressed via whereClause.
responseHandler a block (closure) to handle successful result of an asynchronous call.
errorHandler a block (closure) fault result of an asynchronous call.

Return Value

The API returns the number of objects updated in the database as a result of the request.

Codeless Reference

data_service_bulk_update

where:

Argument                Description
table name Name of the data table where records must be updated.
where clause A condition which references a column name and a specific value in the data table where an update has to be carried out. Refer to the Search With The Where Clause topic for more information.
changes This parameter expects an object that must contain the name of the column and a new value to update the existing contents with.
Return count of updated objects Optional parameter. When this box is checked, the operation returns the number of updated objects.

Returns the number of updated records.

Consider the following records in the employees data table:
data_service_example_bulk_update_1

In order to select data that must be updated, the example below sets a condition(where clause) for update operation to "ContactType = 'Personal'". As you can see in the screenshot above, the contactType is the column where two string values Personal are stored. By setting a condition, the method gets instructed to apply an update in a specific place(contactType column) and for a specific value(Personal).

data_service_example_bulk_update_2

The result of this operation will look as shown below after the Codeless logic runs. The values in the contactType column were changed from Personal to Work.

data_service_example_bulk_update_3

Moreover, the operation has returned the number 2, indicating the number of updated objects in the employees data table.

data_service_example_bulk_update_4