Make sure to sanitize client data to protect against code injection when using Functions.
Consider an Atlas App Services Function named concatenate
that takes two arguments, concatenates them, and returns the result:
exports = function(a, b) { return a + b;};
To execute a function from the Swift SDK, use the functions
object on the currently logged-in user.
The functions
object has dynamic members corresponding to functions. In this case, functions.concatenate()
refers to the concatenate
function. Pass a BSONArray
of arguments. 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
.
RLMApp *app = [RLMApp appWithId:YOUR_APP_ID];RLMUser *user = [app currentUser];[user callFunctionNamed:@"concatenate" arguments:@[@"john.smith", @"@companyemail.com"] completionBlock:^(id<RLMBSON> result, NSError *error) { if (error) { NSLog(@"Function call failed: %@", [error localizedDescription]); return; } NSLog(@"Called function 'concatenate' and got result: %@", result); assert([result isEqual:@"john.smith@companyemail.com"]);}];
let app = App(id: YOUR_APP_SERVICES_APP_ID)let user = app.currentUser!user.functions.concatenate([AnyBSON("john.smith"), AnyBSON("@companyemail.com")]) { concatenate, error in guard error == nil else { print("Function call failed: \(error!.localizedDescription)") return } guard case let .string(value) = concatenate else { print("Unexpected non-string result: \(concatenate ?? "nil")") return } print("Called function 'concatenate' and got result: \(value)") assert(value == "john.smith@companyemail.com")}
New in version 10.16.0.
The Realm Swift SDK offers async/await versions of the User.function
methods.
func testAsyncCallFunction() async { let app = App(id: YOUR_APP_SERVICES_APP_ID) let user = app.currentUser! do { let concatenatedString = try await user.functions.concatenate([AnyBSON("john.smith"), AnyBSON("@companyemail.com")]) print("Called function 'concatenate' and got result: \(concatenatedString)") assert(concatenatedString == "john.smith@companyemail.com") } catch { print("Function call failed: \(error.localizedDescription)") }}
Starting with Realm Swift SDK Versions 10.15.0 and 10.16.0, many of the Realm APIs support the Swift async/await syntax. Projects must meet these requirements:
Swift SDK Version
Swift Version Requirement
Supported OS
10.25.0
Swift 5.6
iOS 13.x
10.15.0 or 10.16.0
Swift 5.5
iOS 15.x
If your app accesses Realm in an async/await
context, mark the code with @MainActor
to avoid threading-related crashes.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4