Call a Function

You can call a function from a connected client application.

The examples in this section demonstrate calling a simple function named sum that takes two arguments, adds them, and returns the result:

// sum: adds two numbers
exports = function(a, b) {
return a + b;

To execute a function from the iOS Client SDK, use the functions method of the app.

let app = App(id: YOUR_REALM_APP_ID)
// ... log in ...
let user = app.currentUser!
// The dynamic member name `sum` is directly associated with the function
// name. The first argument is the `BSONArray` of arguments to be provided
// to the function. The trailing closure is the completion handler
// to call when the function call is complete. This handler is executed on
// a non-main global `DispatchQueue`.
user.functions.sum([1, 2]) { sum, error in
guard error == nil else {
print("Function call failed: \(error!.localizedDescription)")
guard case let .double(value) = sum else {
print("Unexpected non-double result: \(sum ?? "nil")");
print("Called function 'sum' and got result: \(value)")
assert(value == 3)
Give Feedback