Skip to content

Saving multiple objects

This is a transaction operation responsible for saving one or more new objects in the database. To add this operation to a Unit Of Work instance, use the following methods:

// custom class-based approach
- (OpResult *)[unitOfWorkInstance bulkCreateWithObjectsToSave:(NSArray * _Nonnull)];

// map-based approach
- (OpResult *)[unitOfWorkInstance bulkCreateWithTableName:(NSString * _Nonnull) objectsToSave:(NSArray<NSDictionary<NSString *,id> *> * _Nonnull)];
// custom class-based approach
unitOfWorkInstance.bulkCreate(objectsToSave: [Any]) -> OpResult

// dictionary-based approach
unitOfWorkInstance.bulkCreate(tableName: String, objectsToSave: [[String : Any]]) -> OpResult

The method argument is a collection containing objects which will be stored in the database. As you can see from the method signatures, there are two approaches for storing new objects: class-based and map-based approaches. The former uses a class defined in your program. The signature defines that class as type Any. In this case, the name of the table where the object will be stored is the same as the name of the class. The objectsToSave argument in both signatures is a collection of objects to save. Each object must conform to the rules defined in the Data Object section of this guide.

Return Value

The operation returns an OpResult object which represents the result of this operation - a collection of objectId values. Each element in the resulting collection is a string value. It is the objectId of the corresponding object stored in the database. The OpResult object can be used as an argument in other operations in the same transaction (same UnitOfWork instance). It is also possible to use OpResult to "extract" the value of a property of the saved object and use it in other operations. For more information see the Operation Result chapter of this guide.

Example

Consider the example below. In the example three objects are added to a collection. The collection is then added to a transaction with the bulkCreate method.

@interface Person : NSObject

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

@end

UnitOfWork *unitOfWork = [UnitOfWork new];

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

Person *person2 = [Person new];
person2.name = @"Mary";
person2.age = 32;

Person *person3 = [Person new];
person3.name = @"Bob";
person3.age = 22;

NSArray *people = @[person1, person2, person3];

[unitOfWork bulkCreateWithObjectsToSave:people];

// add other operations
// ...

// use [unitOfWork execute] to run the transaction
@objcMembers class Person: NSObject {    
    var name: String?
    var age: Int = 0
}
let unitOfWork = UnitOfWork()

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

let person2 = Person()
person2.name = "Mary"
person2.age = 32

let person3 = Person()
person3.name = "Bob"
person3.age = 22

let people = [person1, person2, person3]

let _ = unitOfWork.bulkCreate(objectsToSave: people)

// add other operations
// ...

// use unitOfWork.execute() to run the transaction

UnitOfWork *unitOfWork = [UnitOfWork new];

NSDictionary *person1 = @{@"name": @"Joe", @"age": @25};
NSDictionary *person2 = @{@"name": @"Mary", @"age": @32};
NSDictionary *person3 = @{@"name": @"Bob", @"age": @22};
NSArray *people = @[person1, person2, person3];

[unitOfWork bulkCreateWithTableName:@"Person" objectsToSave:people];

// add other operations
// ...

// use [unitOfWork execute] to run the transaction
let unitOfWork = UnitOfWork()

let person1 = ["name": "Joe", "age": 25] as [String : Any]
let person2 = ["name": "Mary", "age": 32] as [String : Any]
let person3 = ["name": "Bob", "age": 22] as [String : Any]
let people = [person1, person2, person3]

let _ = unitOfWork.bulkCreate(tableName: "Person", objectsToSave: people)

// add other operations
// ...

// use unitOfWork.execute() to run the transaction