Skip to content

Overview

Backendless Counters API provides a centralized server-side facility for working with values that may be updated atomically. Every counter has a name assigned to it. The name is used in all operations to identify the counter. Since the counter value is managed centrally, multiple heterogeneous clients can access and modify the value.

For example, the following code increments a counter and returns the current value:

[Backendless.shared.counters incrementAndGetWithCounterName:@"MyCounter" responseHandler:^(NSInteger value) {
    NSLog(@"Couner value = %li", value);
} errorHandler:^(Fault *fault) {
    NSLog(@"Error: %@", fault.message);
}];
Backendless.shared.counters.incrementAndGet(counterName: "MyCounter", responseHandler: { value in
    print("Couner value = \(value)")
}, errorHandler: { fault in
    print("Error: \(fault.message ?? "")")
})

Additionally, there is a shortcut approach:

id<IAtomic> myCounter = [Backendless.shared.counters ofCounterName:@"MyCounter"];
[myCounter incrementAndGetWithResponseHandler:^(NSInteger value) {
    NSLog(@"Couner value = %li", (long)value);
} errorHandler:^(Fault *fault) {
    NSLog(@"Error: %@", fault.message);
}];
let myCounter = Backendless.shared.counters.of(counterName: "MyCounter")
myCounter.incrementAndGet(responseHandler: { value in
    print("Counter value = \(value)")
}, errorHandler: { fault in
    print("Error: \(fault.message ?? "")")
})