Deleting a single object¶
Important
This operation deletes a single object in the database. To delete more than one object, use the bulkDelete operation documented in the Deleting multiple objects chapter of this guide.
To add this operation to a Unit Of Work instance, use the following methods. The methods are organized by how the object to be deleted is identified:
// Delete the object represented as a Map from the table with the name of tableName.
// The mapBasedObject collection must have a key/value pair for the "objectId" key.
- (OpResult *)[unitOfWorkInstance
deleteWithTableName:(NSString * _Nonnull)
objectToDelete:(NSDictionary<NSString *,id> * _Nonnull)];
// Delete the object represented as an instance of a custom class.
// Backendless deletes the object from a table with the same name
// as the name of the class. For example, if YourCustomClass is a
// class called Person, the the object will be deleted from the
// table Person. YourCustomClass must have public get/set method
// for the objectId property or a public objectId field.
- (OpResult *)[unitOfWorkInstance deleteWithObjectToDelete:(YourCustomClass * _Nonnull)];
// Delete an object identified by the objectId argument from the
// table with the name of tableName
- (OpResult *)[unitOfWorkInstance
deleteWithTableName:(NSString * _Nonnull)
objectId:(NSString * _Nonnull)];
// Delete an object which is a result of a prior operation in the
// same transaction. This operation expects a single object, which
// means the "result" argument can be the result from any of the
// following operations:
// - saving a single object
// - updating a single object
- (OpResult *)[unitOfWorkInstance deleteWithResult:(OpResult * _Nonnull)];
// Delete an object represented by a result from another operation
// in the same transaction. The difference between this method and
// the one above is the OpResultValueReference object represents
// a value within an OpResult object. For example, an OpResult may
// be a collection of objects from the "Retrieving objects" operation.
// A single object from that collection is represented by a
// OpResultValueReference instance used in this method. The
// "valueReference" argument can be an object from a result from any
// of the following operations:
// - retrieving objects
// - saving multiple objects
- (OpResult *)[unitOfWorkInstance
deleteWithValueReference:(OpResultValueReference * _Nonnull)];
// Delete the object represented as a dictionary from the table with the name of tableName.
// The objectToDelete collection must have a key/value pair for the "objectId" key.
unitOfWorkInstance.delete(tableName: String, objectToDelete: [String : Any]) -> OpResult
// Delete the object represented as an instance of a custom class.
// Backendless deletes the object from a table with the same name
// as the name of the class. For example, if YourCustomClass is a
// class called Person, the the object will be deleted from the
// table Person. YourCustomClass must have public get/set method
// for the objectId property or a public objectId field.
unitOfWorkInstance.delete(objectToDelete: YourCustomClass) -> OpResult
// Delete an object identified by the objectId argument from the
// table with the name of tableName
unitOfWorkInstance.delete(tableName: String, objectId: String) -> OpResult
// Delete an object which is a result of a prior operation in the
// same transaction. This operation expects a single object, which
// means the "result" argument can be the result from any of the
// following operations:
// - saving a single object
// - updating a single object
unitOfWorkInstance.delete(result: OpResult) -> OpResult
// Delete an object represented by a result from another operation
// in the same transaction. The difference between this method and
// the one above is the OpResultValueReference object represents
// a value within an OpResult object. For example, an OpResult may
// be a collection of objects from the "Retrieving objects" operation.
// A single object from that collection is represented by a
// OpResultValueReference instance used in this method. The
// "valueReference" argument can be an object from a result from any
// of the following operations:
// - retrieving objects
// - saving multiple objects
unitOfWorkInstance.delete(valueReference: OpResultValueReference) -> OpResult
Return Value¶
All methods return an instance of the OpResult
class which represents the timestamp when the object was deleted from the database. The 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:
// create unit of work - it represents the transaction
UnitOfWork *unitOfWork = [UnitOfWork new];
// create a map-based object.
// the only required property is objectId.
// in your application the object may come from
// an API used outside of the transaction, or you
// can compose it the same way as shown below
NSDictionary *orderObject = @{@"objectId": @"EB68A9E1-DC24-C961-FFC4-2EF9227BF700"};
// add delete operation to the transaction
[unitOfWork deleteWithTableName:@"Order" objectToDelete:orderObject];
// 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];
// create a class-based object.
// the only required property is objectId.
// in your application the object may come from
// an API used outside of the transaction, or you
// can compose it the same way as shown below
Order *order = [Order new];
order.objectId = @"996E2034-4CC6-0BC4-FF01-E92077819700";
// add delete operation to the transaction
[unitOfWork deleteWithObjectToDelete:order];
// run the transaction
[unitOfWork executeWithResponseHandler:^(UnitOfWorkResult *result) {
NSLog(@"Transaction complete - %@", result);
} errorHandler:^(Fault *fault) {
NSLog(@"Error: %@", fault.message);
}];
@interface Order : NSObject
@property (strong, nonatomic) NSString *objectId;
@property (strong, nonatomic) NSDate *deliveryDate;
@property (strong, nonatomic) NSString *orderStatus;
@end
// create unit of work - it represents the transaction
UnitOfWork *unitOfWork = [UnitOfWork new];
// objectId of the object to delete.
NSString *objectId = @"37ADFFA8-39E2-73A5-FF44-651EF5C4E900";
// add delete operation to the transaction
[unitOfWork deleteWithTableName:@"Order" objectId:objectId];
// 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];
DataQueryBuilder *queryBuilder = [DataQueryBuilder new];
queryBuilder.whereClause = @"orderStatus = 'invalid'";
OpResult *invalidOrders = [unitOfWork findWithTableName:@"Order" queryBuilder:queryBuilder];
OpResultValueReference *firstInvalidOrder = [invalidOrders resolveToResultIndex:@0];
// add delete operation to the transaction
[unitOfWork deleteWithValueReference:firstInvalidOrder];
// 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
let unitOfWork = UnitOfWork()
// create a map-based object.
// the only required property is objectId.
// in your application the object may come from
// an API used outside of the transaction, or you
// can compose it the same way as shown below
let orderObject = ["objectId": "EB68A9E1-DC24-C961-FFC4-2EF9227BF700"]
// add delete operation to the transaction
let _ = unitOfWork.delete(tableName: "Order", objectToDelete: orderObject)
// 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()
// create a class-based object.
// the only required property is objectId.
// in your application the object may come from
// an API used outside of the transaction, or you
// can compose it the same way as shown below
let order = Order()
order.objectId = "996E2034-4CC6-0BC4-FF01-E92077819700"
// add delete operation to the transaction
let _ = unitOfWork.delete(objectToDelete: order)
// run the transaction
unitOfWork.execute(responseHandler: { result in
print("Transaction complete - \(result)")
}, errorHandler: { fault in
print("Error: \(fault.message ?? "")")
})
@objcMembers class Order: NSObject {
public var objectId: String?
public var deliveryDate: Date?
public var orderStatus: String?
}
// create unit of work - it represents the transaction
let unitOfWork = UnitOfWork()
// objectId of the object to delete.
let objectId = "37ADFFA8-39E2-73A5-FF44-651EF5C4E900"
// add delete operation to the transaction
let _ = unitOfWork.delete(tableName: "Order", objectId: objectId)
// 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()
let queryBuilder = DataQueryBuilder()
queryBuilder.whereClause = "orderStatus = 'invalid'"
let invalidOrders = unitOfWork.find(tableName: "Order", queryBuilder: queryBuilder)
let firstInvalidOrder = invalidOrders.resolveTo(resultIndex: 0)
// add delete operation to the transaction
let _ = unitOfWork.delete(valueReference: firstInvalidOrder)
// run the transaction
unitOfWork.execute(responseHandler: { result in
print("Transaction complete - \(result)")
}, errorHandler: { fault in
print("Error: \(fault.message ?? "")")
})