Skip to content

Saving Multiple Objects

This API stores multiple objects in a data table with a single request. Consider the following example, it demonstrates an API call which saves two objects in the Person data table:

// Dictionary/Map Approach

[[Backendless.shared.data ofTable:@"TABLE-NAME"] bulkCreateWithEntities:@[NSDictionary, NSDictionary, ...] responseHandler:^(NSArray *objectIdArray) {
    // handle response
} errorHandler:^(Fault *fault) {
    // handle error
}];


//Custom Class Approach

[[Backendless.shared.data of:[DataTypeX class]] bulkCreateWithEntities:[DataTypeX(), DataTypeX(), ...] responseHandler:^(NSArray *objectIdArray) {
    // handle response
} errorHandler:^(Fault *fault) {
    // handle error
}];
// Dictionary/Map Approach

Backendless.shared.data.ofTable("TABLE-NAME").bulkCreate(entities: [[String : Any], [String : Any], ...], responseHandler: { objectIdArray in
    // handle response
}, errorHandler: { fault in
    // handle error
})


// Custom Class Approach

Backendless.shared.data.of(DataTypeX.self).bulkCreate(entities: [DataTypeX(), DataTypeX(),...], responseHandler: { objectIdArray in
    // handle response
}, errorHandler: { fault in
    // handle error
})

The result of running the sample above is two objects saved in the database:

bulk-create-result.zoom80

Available methods

@interface Person : NSObject

@property NSString *name;
@property NSInteger age;

@end

// -------------------

Person *person1 = [Person new];
person1.name = @"Joe";
person1.age = 24;

Person *person2 = [Person new];
person2.name = @"Betty";
person2.age = 34;

NSArray *entities = @[person1, person2];

[[Backendless.shared.data of:[Person class]] bulkCreateWithEntities:entities responseHandler:^(NSArray *result) {
    NSLog(@"Objects have been saved");
} errorHandler:^(Fault *fault) {
    NSLog(@"Error: %@", fault.message);
}];
@objcMembers class Person: NSObject {
    var name: String?
    var age: Int = 0
}

// -------------------

let person1 = Person()
person1.name = "Joe"
person1.age = 24

let person2 = Person()
person2.name = "Betty"
person2.age = 34

let people = [person1, person2]

Backendless.shared.data.of(Person.self).bulkCreate(entities: people, responseHandler: { result in
    print("Objects have been saved")
}, errorHandler: { fault in
    print("Error: \(fault.message ?? "")")
})
NSArray *people = @[
    @{@"name": @"Joe", @"age": @24},
    @{@"name": @"Betty", @"age": @34}
];

[[Backendless.shared.data ofTable:@"Person"] bulkCreateWithEntities:people responseHandler:^(NSArray *result) {
    NSLog(@"Objects have been saved");
} errorHandler:^(Fault *fault) {
    NSLog(@"Error: %@", fault.message);
}];
let people = [
    ["name": "Joe", "age": 24],
    ["name": "Betty", "age": 34],
] as [[String : Any]]

Backendless.shared.data.ofTable("Person").bulkCreate(entities: people, responseHandler: { result in
    print("Objects have been saved")
}, errorHandler: { fault in
    print("Error: \(fault.message ?? "")")
})

where:

Argument                Description
entities objects to save in the database.
responseHandler a block (closure) to handle successful result of an asynchronous call.
errorHandler a block (closure) to handle fault result of an asynchronous call.

Return Value

The API returns a collection of object IDs for the objects created in the database. The order of  the objectId values in the response matches the order of the objects in the request.

The Backendless server implementation enforces the following rules:

Rules
All objects in the request must be of the same type.
Objects in the request may have different set of properties. A property in the request body is ignored, if a column does not exist in the table.
Objects in the request may have a unique objectId value that is not used in the database.
Maximum number of objects in a single request is 100 for Backendless Cloud. It is configurable for Backendless Pro and Managed Backendless.
A request will be rejected by the server, if there is no Create permission granted to the user identity/roles associated with the request.

Codeless Reference

data_service_bulk_save

where:

Argument                Description
table name Name of the data table where new records must be saved.
objects Must be a list containing objects to save in the database. Object properties must match the names of the table columns. The object must not have the objectId property.
Return created objectIds Optional parameter. When this option is selected, the operation returns a list containing one or more objectId values , depending on the number of saved objects.

Returns a list containing one or more objectId values, depending on the number of saved objects.

Consider the following structure of the data table called employees:
data_service_example_saving_object_1

For demonstration purposes, the data table presented above has three custom columns: name, position, and phoneNumber. The objectId is a system column that contains unique identifiers of the records in the table. When a new record is saved to the table, the system assigns a unique objectId to it. The objectid is used in various operations as an access point to a specific record.

The example below saves two new objects to the employees data table:

data_service_example_bulk_save_1

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

data_service_example_bulk_save_2

The operation described above has returned a list containing two objectId values associated with the newly created records:

data_service_example_bulk_save_3