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;

Call from a Client Application

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

let app = App(id: "my-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: \(value)")
    assert(value == 3)