Skip to content

Conditional Delivery of New Objects

Consider the following data table. The table name is Order and it contains the orderAmount column which will be used in the example:

order-table-rt.zoom70

The code below adds a listener for the create event for the Order table.

EventHandlerForClass *eventHandler = [Backendless.shared.data of:[Order class]].rt;
RTSubscription *subscription = [eventHandler addCreateListenerWithWhereClause:@"orderAmount > 1000" responseHandler:^(Order *createdObject) {
    NSLog(@"Order with orderAmount > 1000 has been created: %@", createdObject);
} errorHandler:^(Fault *fault) {
    NSLog(@"Error: %@", fault.message);
}];
let eventHandler = Backendless.shared.data.of(Order.self).rt
let subscription = eventHandler?.addCreateListener(whereClause: "orderAmount > 1000", responseHandler: { createdObject in
    print("Order with orderAmount > 1000 has been created: \(createdObject)")
}, errorHandler: { fault in
    print("Error: \(fault.message ?? "")")
})

EventHandlerForMap *eventHandler = [Backendless.shared.data ofTable:@"Order"].rt;
RTSubscription *subscription = [eventHandler addCreateListenerWithWhereClause:@"orderAmount > 1000" responseHandler:^(NSDictionary *createdObject) {
    NSLog(@"Order with orderAmount > 1000 has been created: %@", createdObject);
} errorHandler:^(Fault *fault) {
    NSLog(@"Error: %@", fault.message);
}];
let eventHandler = Backendless.shared.data.ofTable("Order").rt
let subscription = eventHandler?.addCreateListener(whereClause: "orderAmount > 1000", responseHandler: { createdObject in
    print("Order with orderAmount > 1000 has been created: \(createdObject)")
}, errorHandler: { fault in
    print("Error: \(fault.message ?? "")")
})

The event listener will receive only the Order objects where the amount of order is greater than 1000. This is achieved with the first argument in the addCreateListener method. The argument is a where clause condition, which references the orderAmount column and requests that the real-time database sends only the matching objects. The second argument is a callback object which will receive any new object which matches the where clause condition. Notice the argument of the response callback method is the actual object saved in the database.

For more information about the whereClause syntax, see the Condition Syntax section of the guide.