Navigation

Call a Function

Overview

You can call a function from a connected client application.

Usage

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 = RealmApp(id: "my-realm-app-id")

// 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 second and final argument is the completion handler
// to call when the function call is complete. This handler is executed on
// a non-main global `DispatchQueue`.
app.functions.sum([1, 2, 3, 4, 5]) { sum, error in
    guard case let .int64(value) = sum else {
        print(error?.localizedDescription)
    }
    assert(value == 15)
}
←   Sync Data Auxiliary Files  →