Skip to content

Deleting multiple objects

This operation deletes multiple objects from the database. To add this operation to a Unit Of Work instance, use the following methods. The methods are organized by how the objects to be deleted are identified:

// delete multiple objects identified by using their objectId values
// in the objectIdValues array. All referenced objects in the objectIdValues
// argument must be stored in a table with name of tableName.
-(OpResult *)[unitOfWorkInstance
              bulkDeleteWithTableName:(NSString * _Nonnull)
              objectIdValues:(NSArray<NSString *> * _Nonnull)];
// delete multiple objects represented by instances of the
// Dictionary interface in the "objectsToDelete" collection.
// Each Map object must have the "objectId" key with a valid
// value to the object in the database. Objects must be stored
// in the "tableName" table.
- (OpResult *)[unitOfWorkInstance
               bulkDeleteWithTableName:(NSString * _Nonnull)
               objectsToDelete:(NSArray<NSDictionary<NSString *,id> *> * _Nonnull)];
// delete multiple objects identified by their objectId values.
// Each object is represented by an instance of YourCustomClass.
// The class must have the public String objectId field with a 
// valid value. Objects will be deleted in a table which corresponds
// to the YourCustomClass class. This means that if YourCustomClass is
// called Person, the objects will be deleted from the Person
// table. If a referenced object is not stored in the table
// the operation returns an error and the entire transaction
// rolls back.
- (OpResult *)[unitOfWorkInstance
               bulkDeleteWithObjectsToDelete:(NSArray * _Nonnull)];
// Delete all objects that match the "whereClause" query
// in a table with the tableName name.
- (OpResult *)[unitOfWorkInstance
               bulkDeleteWithTableName:(NSString * _Nonnull)
               whereClause:(NSString * _Nonnull)];
// Delete all objects identified by the "result" object. This must
// be a result from another operation in the same transaction. The
// "result" object must represent a collection of objects to
// delete. It can be a result of any of the following operations:
//   - retrieving objects
//   - saving multiple objects
- (OpResult *)[unitOfWorkInstance bulkDeleteWithResult:(OpResult * _Nonnull)];

// delete multiple objects identified by using their objectId values
// in the objectIdValues array. All referenced objects in the objectIdValues
// argument must be stored in a table with name of tableName.
unitOfWorkInstance.bulkDelete(tableName: String, 
                              objectIdValues: [String]) -> OpResult
// delete multiple objects represented by instances of the
// Map interface in the "objectsToDelete" collection.
// Each dictionary object must have the "objectId" key with a valid
// value to the object in the database. Objects must be stored
// in the "tableName" table.
unitOfWorkInstance.bulkDelete(tableName: String, 
                              objectsToDelete: [[String : Any]]) -> OpResult
// delete multiple objects identified by their objectId values.
// Each object is represented by an instance of YourCustomClass.
// The class must have the public String objectId field with a valid 
// value. Objects will be deleted in a table which corresponds to the
// YourCustomClass class. This means that if YourCustomClass is
// called Person, the objects will be deleted from the Person
// table. If a referenced object is not stored in the table
// the operation returns an error and the entire transaction
// rolls back.
unitOfWorkInstance.bulkDelete(objectsToDelete: [YourCustomClass]) -> OpResult
// Delete all objects that match the "whereClause" query
// in a table with the tableName name.
unitOfWorkInstance.bulkDelete(tableName: String, whereClause: String) -> OpResult
// Delete all objects identified by the "result" object. This must
// be a result from another operation in the same transaction. The
// "result" object must represent a collection of objects to
// delete. It can be a result of any of the following operations:
//   - retrieving objects
//   - saving multiple objects
unitOfWorkInstance.bulkDelete(result: OpResult) -> OpResult

Return Value

All methods listed above return an instance of the OpResult class which represents the number of objects deleted in the database. The returned OpResult object can be used as an argument in other operations in the same transaction (same UnitOfWork instance). For more information see the Operation Result chapter of this guide.

Example

The examples below demonstrate the usage of the operation for all possible ways to reference the object that should be deleted:

UnitOfWork *unitOfWork = [UnitOfWork new];

// compose a collection of objectId's to delete
NSArray *objectsToDelete = @[@"5220681E-AB99-D53C-FF6C-C97ED0C65D00",
                             @"C2937EA2-182A-49BC-FFDD-3A34B41CF200",
                             @"B4E120E5-1990-22B2-FF80-AD0AE9ACB100"];

// add the bulkDelete operation to transaction
[unitOfWork bulkDeleteWithTableName:@"Order"
                     objectIdValues:objectsToDelete];

// run the transaction
[unitOfWork executeWithResponseHandler:^(UnitOfWorkResult *result) {
    NSLog(@"Transaction complete - %@", result);
} errorHandler:^(Fault *fault) {
    NSLog(@"Error: %@", fault.message);
}];
UnitOfWork *unitOfWork = [UnitOfWork new];

// create a collection of dictionary objects
NSMutableArray *ordersToDelete = [NSMutableArray new];

// here we construct map objects and populate them with
// the objectId values. It is the only required property
// for the deletion operation. Instead of creating these
// objects, they could be coming from the server from
// another API call.
NSDictionary *order1 = @{@"objectId": @"D426CE1C-ACAF-B352-FF7D-4881EF280700"};
[ordersToDelete addObject:order1];

NSDictionary *order2 = @{@"objectId": @"81556273-55A4-0C8C-FF85-6A32EC017B00"};
[ordersToDelete addObject:order2];

NSDictionary *order3 = @{@"objectId": @"0442109E-15A8-3609-FFFE-D53ECF2A6300"};
[ordersToDelete addObject:order3];

// add the bulkDelete operation to transaction
[unitOfWork bulkDeleteWithTableName:@"Order"
                    objectsToDelete:ordersToDelete];

// run the transaction
[unitOfWork executeWithResponseHandler:^(UnitOfWorkResult *result) {
    NSLog(@"Transaction complete - %@", result);
} errorHandler:^(Fault *fault) {
    NSLog(@"Error: %@", fault.message);
}];
UnitOfWork *unitOfWork = [UnitOfWork new];

// create a collection of Order objects
NSMutableArray *ordersToDelete = [NSMutableArray new];

// here we construct Order objects and populate them with
// the objectId values. It is the only required property
// for the deletion operation. Instead of creating these
// objects, they could be coming from the server from
// another API call.
Order *order1 = [Order new];
order1.objectId = @"1D0A453F-BE4E-ADE0-FFDB-8815368EB400";
[ordersToDelete addObject:order1];

Order *order2 = [Order new];
order2.objectId = @"F176163C-9F1D-BFEB-FF0C-99F311735400";
[ordersToDelete addObject:order2];

Order *order3 = [Order new];
order3.objectId = @"668BF86B-14F6-B6A9-FF5E-BE999C497900";
[ordersToDelete addObject:order3];

// add the bulkDelete operation to transaction
[unitOfWork bulkDeleteWithObjectsToDelete:ordersToDelete];

// run the transaction
[unitOfWork executeWithResponseHandler:^(UnitOfWorkResult *result) {
    NSLog(@"Transaction complete - %@", result);
} errorHandler:^(Fault *fault) {
    NSLog(@"Error: %@", fault.message);
}];
// create unit of work - it represents the transaction
UnitOfWork *unitOfWork = [UnitOfWork new];

// add delete operation to the transaction.
// The first argument is the name of the table - "Order"
// The second argument is a query used to identify the
// order which should be deleted. Any order that has
// the orderStatus set to "invalid", will be deleted
// by this operation
[unitOfWork bulkDeleteWithTableName:@"Order" whereClause:@"orderStatus = 'invalid'"];

// run the transaction
[unitOfWork executeWithResponseHandler:^(UnitOfWorkResult *result) {
    NSLog(@"Transaction complete - %@", result);
} errorHandler:^(Fault *fault) {
    NSLog(@"Error: %@", fault.message);
}];
// create unit of work - it represents the transaction
UnitOfWork *unitOfWork = [UnitOfWork new];

// compose a query. It will fetch all objects which
// have orderStatus set to "invalid".
DataQueryBuilder *queryBuilder = [DataQueryBuilder new];
queryBuilder.whereClause = @"orderStatus = 'invalid'";

// add the find operation to the transaction.
// Notice the result is stored in the "invalidOrder" variable
OpResult *invalidOrders = [unitOfWork findWithTableName:@"Order" queryBuilder:queryBuilder];

// add delete operation to the transaction.
// Notice the argument is the result from the previous operation
// in the transaction
[unitOfWork bulkDeleteWithResult:invalidOrders];

// run the transaction
[unitOfWork executeWithResponseHandler:^(UnitOfWorkResult *result) {
    NSLog(@"Transaction complete - %@", result);
} errorHandler:^(Fault *fault) {
    NSLog(@"Error: %@", fault.message);
}];

let unitOfWork = UnitOfWork()

// compose a collection of objectId's to delete
let objectsToDelete = ["5220681E-AB99-D53C-FF6C-C97ED0C65D00",
                       "C2937EA2-182A-49BC-FFDD-3A34B41CF200",
                       "B4E120E5-1990-22B2-FF80-AD0AE9ACB100"]

// add the bulkDelete operation to transaction
let _ = unitOfWork.bulkDelete(tableName: "Order", objectIdValues: objectsToDelete)

// run the transaction
unitOfWork.execute(responseHandler: { result in
    print("Transaction complete - \(result)")
}, errorHandler: { fault in
    print("Error: \(fault.message ?? "")")
})
let unitOfWork = UnitOfWork()

// create a collection of dictionary objects
var ordersToDelete = [[String : Any]]()

// here we construct map objects and populate them with
// the objectId values. It is the only required property
// for the deletion operation. Instead of creating these
// objects, they could be coming from the server from
// another API call.
let order1 = ["objectId": "D426CE1C-ACAF-B352-FF7D-4881EF280700"]
ordersToDelete.append(order1)

let order2 = ["objectId": "81556273-55A4-0C8C-FF85-6A32EC017B00"]
ordersToDelete.append(order2)

let order3 = ["objectId": "0442109E-15A8-3609-FFFE-D53ECF2A6300"]
ordersToDelete.append(order3)

// add the bulkDelete operation to transaction
let _ = unitOfWork.bulkDelete(tableName: "Order", objectsToDelete: ordersToDelete)

// run the transaction
unitOfWork.execute(responseHandler: { result in
    print("Transaction complete - \(result)")
}, errorHandler: { fault in
    print("Error: \(fault.message ?? "")")
})
let unitOfWork = UnitOfWork()

// create a collection of Order objects
var ordersToDelete = [Order]()

// here we construct Order objects and populate them with
// the objectId values. It is the only required property
// for the deletion operation. Instead of creating these
// objects, they could be coming from the server from
// another API call.
let order1 = Order()
order1.objectId = "1D0A453F-BE4E-ADE0-FFDB-8815368EB400"
ordersToDelete.append(order1)

let order2 = Order()
order2.objectId = "F176163C-9F1D-BFEB-FF0C-99F311735400"
ordersToDelete.append(order2)

let order3 = Order()
order3.objectId = "668BF86B-14F6-B6A9-FF5E-BE999C497900"
ordersToDelete.append(order3)

// add the bulkDelete operation to transaction
let _ = unitOfWork.bulkDelete(objectsToDelete: ordersToDelete)

// run the transaction
unitOfWork.execute(responseHandler: { result in
    print("Transaction complete - \(result)")
}, errorHandler: { fault in
    print("Error: \(fault.message ?? "")")
})
// create unit of work - it represents the transaction
let unitOfWork = UnitOfWork()

// add delete operation to the transaction.
// The first argument is the name of the table - "Order"
// The second argument is a query used to identify the
// order which should be deleted. Any order that has
// the orderStatus set to "invalid", will be deleted
// by this operation
let _ = unitOfWork.bulkDelete(tableName: "Order", whereClause: "orderStatus = 'invalid'")

// run the transaction
unitOfWork.execute(responseHandler: { result in
    print("Transaction complete - \(result)")
}, errorHandler: { fault in
    print("Error: \(fault.message ?? "")")
})
// create unit of work - it represents the transaction
let unitOfWork = UnitOfWork()

// compose a query. It will fetch all objects which
// have orderStatus set to "invalid".
let queryBuilder = DataQueryBuilder()
queryBuilder.whereClause = "orderStatus = 'invalid'"

// add the find operation to the transaction.
// Notice the result is stored in the "invalidOrder" variable
let invalidOrders = unitOfWork.find(tableName: "Order", queryBuilder: queryBuilder)

// add delete operation to the transaction.
// Notice the argument is the result from the previous operation
// in the transaction
let _ = unitOfWork.bulkDelete(result: invalidOrders)

// run the transaction
unitOfWork.execute(responseHandler: { result in
    print("Transaction complete - \(result)")
}, errorHandler: { fault in
    print("Error: \(fault.message ?? "")")
})