Skip to content

Upsert Multiple Objects

Description

This operation creates and/or updates multiple objects in the database. The operation receives a collection of objects as the argument. For every object in the collection it checks if an object already exists in the database and in that case updates it with the property values in the object. Otherwise, the object is saved in the database. To check if an object exists in the database, Backendless uses the value of the objectId property.

Method

- (void)bulkUpsertWithEntities:(NSArray<id> *)entities responseHandler:(void (^)(NSArray<NSString *> *))responseHandler errorHandler:(void (^)(Fault *))errorHandler;
func bulkUpsert(entities: [Any], responseHandler: (([String]) -> Void)!, errorHandler: ((Fault) -> Void)!)
- (void)bulkUpsertWithEntities:(NSArray<NSDictionary<NSString *, id> *> *)entities responseHandler:(void (^)(NSArray<NSString *> *))responseHandler errorHandler:(void (^)(Fault *))errorHandler;
func bulkUpsert(entities: [[String : Any]], responseHandler: (([String]) -> Void)!, errorHandler: ((Fault) -> Void)!)

where:

Argument                Description
entities Required parameter.A dictionary/map containing objects to upsert.

Return Value

An array of the objectId values of updated or inserted objects.

Example

The example below upserts two objects in the "Person" data table.

Person.h
@interface Person : NSObject

@property (nonatomic, strong) NSString *objectId;
@property (nonatomic, strong) NSString *email;
@property NSInteger age;

@end

// *************************************

Person *person1 = [Person new];
person1.objectId = @"2DC75C06-3F99-4898-B9E8-AA6A83E04012";
person1.email = @"alice@yourmail.com";
person1.age = 25;

Person *person2 = [Person new];
person2.objectId = @"8CCC0E0C-C1AB-4B29-B632-821A00863FA7";
person2.email = @"peter@yourmail.com";
person2.age = 60;

NSArray *people = @[person1, person2];

[[Backendless.shared.data ofTable:@"Person"] bulkUpsertWithEntities:people responseHandler:^(NSArray *ids) {
    NSLog(@"%@", ids);
} errorHandler:^(Fault *fault) {
    NSLog(@"Error: %@)", fault.message);
}];
@objcMembers class Person: NSObject {
    var objectId: String?
    var email: String?
    var age: Int = 0
}

// *************************************

let person1 = Person()
person1.objectId = "2DC75C06-3F99-4898-B9E8-AA6A83E04012"
person1.email = "alice@yourmail.com"
person1.age = 25

let person2 = Person()
person2.objectId = "8CCC0E0C-C1AB-4B29-B632-821A00863FA7"
person2.email = "peter@yourmail.com"
person2.age = 60

let people = [person1, person2]

Backendless.shared.data.of(Person.self).bulkUpsert(entities: people, responseHandler: { ids in
    print(ids)
}, errorHandler: { fault in
    print("Error: \(fault.message ?? "")")
})
NSArray *people = @[
    @{@"objectId": @"2DC75C06-3F99-4898-B9E8-AA6A83E04012", @"email": @"alice@yourmail.com", @"age": @25},
    @{@"objectId": @"8CCC0E0C-C1AB-4B29-B632-821A00863FA7", @"email": @"peter@yourmail.com", @"age": @60}
];

[[Backendless.shared.data ofTable:@"Person"] bulkUpsertWithEntities:people responseHandler:^(NSArray *ids) {
    NSLog(@"%@", ids);
} errorHandler:^(Fault *fault) {
    NSLog(@"Error: %@)", fault.message);
}];
let people = [
    ["objectId": "2DC75C06-3F99-4898-B9E8-AA6A83E04012", "email": "alice@yourmail.com", "age": 25],
    ["objectId": "8CCC0E0C-C1AB-4B29-B632-821A00863FA7", "email": "peter@yourmail.com", "age": 60],
] as [[String : Any]]

Backendless.shared.data.ofTable("Person").bulkUpsert(entities: people, responseHandler: { ids in
    print(ids)
}, errorHandler: { fault in
    print("Error: \(fault.message ?? "")")
})

Codeless Reference

data_api_bulk_upsert_1

where:

Argument                Description
table name Name of the data table where the operation inserts and/or updates objects.
objects A list of objects to update or create.
return upserted objectIds When this box is checked, the operation returns a list of objectId values of updated or inserted objects.

Returns a list containing objectId values of updated or inserted objects.

Consider the following records in the Person data table:

data_api_bulk_upsert_2

The example below updates two objects in the Person data table:

data_api_bulk_upsert_3

The output will look as shown below after the Codeless logic runs. Values in the "age" and "email" columns were modified.

data_api_bulk_upsert_4

The operation returns the array of objectId values:

data_api_bulk_upsert_5