Skip to content

Commit 886934f

Browse files
committed
Update call a function example
1 parent f7a70a9 commit 886934f

File tree

4 files changed

+103
-36
lines changed

4 files changed

+103
-36
lines changed

examples/ios/Examples/Functions.swift

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import XCTest
2+
import RealmSwift
3+
4+
class Functions: XCTestCase {
5+
6+
override func setUp() {
7+
let expectation = XCTestExpectation(description: "logs in")
8+
app.login(credentials: Credentials.anonymous) { (user, error) in
9+
guard error == nil else {
10+
fatalError("Login failed: \(error!.localizedDescription)")
11+
}
12+
expectation.fulfill()
13+
}
14+
wait(for: [expectation], timeout: 10)
15+
}
16+
17+
override func tearDown() {
18+
let expectation = XCTestExpectation(description: "logs out")
19+
app.currentUser!.logOut { (error) in
20+
guard error == nil else {
21+
fatalError("Failed to log out: \(error!.localizedDescription)")
22+
}
23+
expectation.fulfill()
24+
}
25+
wait(for: [expectation], timeout: 10)
26+
}
27+
28+
func testCallFunction() {
29+
let expectation = XCTestExpectation(description: "it completes")
30+
// :code-block-start: call-a-function
31+
let app = App(id: YOUR_REALM_APP_ID)
32+
33+
// ... log in ...
34+
35+
let user = app.currentUser!
36+
37+
// The dynamic member name `sum` is directly associated with the function
38+
// name. The first argument is the `BSONArray` of arguments to be provided
39+
// to the function. The trailing closure is the completion handler
40+
// to call when the function call is complete. This handler is executed on
41+
// a non-main global `DispatchQueue`.
42+
user.functions.sum([1, 2]) { sum, error in
43+
guard error == nil else {
44+
print("Function call failed: \(error!.localizedDescription)")
45+
// :hide-start:
46+
XCTAssertEqual(error!.localizedDescription, "function not found: 'sum'")
47+
expectation.fulfill()
48+
// :hide-end:
49+
return
50+
}
51+
guard case let .double(value) = sum else {
52+
print("Unexpected non-double result: \(sum ?? "nil")");
53+
return
54+
}
55+
print("Called function 'sum' and got result: \(value)")
56+
assert(value == 3)
57+
}
58+
// :code-block-end:
59+
wait(for: [expectation], timeout: 10)
60+
}
61+
}

examples/ios/RealmExamples.xcodeproj/project.pbxproj

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
48F38B502527806600DDEB65 /* AccessMongoDB.swift in Sources */ = {isa = PBXBuildFile; fileRef = 48F38B4F2527806600DDEB65 /* AccessMongoDB.swift */; };
3030
48F38B522527843B00DDEB65 /* CustomUserData.swift in Sources */ = {isa = PBXBuildFile; fileRef = 48F38B512527843B00DDEB65 /* CustomUserData.swift */; };
3131
48F38B5425278ECB00DDEB65 /* CustomUserData.m in Sources */ = {isa = PBXBuildFile; fileRef = 48F38B5325278ECB00DDEB65 /* CustomUserData.m */; };
32+
48F38B562527926500DDEB65 /* Functions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 48F38B552527926500DDEB65 /* Functions.swift */; };
3233
/* End PBXBuildFile section */
3334

3435
/* Begin PBXContainerItemProxy section */
@@ -71,6 +72,7 @@
7172
48F38B4F2527806600DDEB65 /* AccessMongoDB.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AccessMongoDB.swift; sourceTree = "<group>"; };
7273
48F38B512527843B00DDEB65 /* CustomUserData.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CustomUserData.swift; sourceTree = "<group>"; };
7374
48F38B5325278ECB00DDEB65 /* CustomUserData.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = CustomUserData.m; sourceTree = "<group>"; };
75+
48F38B552527926500DDEB65 /* Functions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Functions.swift; sourceTree = "<group>"; };
7476
4B040991139BCE878C9D6C0A /* Pods-RealmExamples.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-RealmExamples.release.xcconfig"; path = "Target Support Files/Pods-RealmExamples/Pods-RealmExamples.release.xcconfig"; sourceTree = "<group>"; };
7577
68EA0F823D8002EF83FC15E5 /* Pods_RealmSwiftExampleTests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_RealmSwiftExampleTests.framework; sourceTree = BUILT_PRODUCTS_DIR; };
7678
6C55D4D8147A41A52A10E3EF /* Pods_RealmObjcExampleTests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_RealmObjcExampleTests.framework; sourceTree = BUILT_PRODUCTS_DIR; };
@@ -131,23 +133,24 @@
131133
4896EE4D2510514B00D1FABF /* Examples */ = {
132134
isa = PBXGroup;
133135
children = (
134-
48924B8B2514FEFC00CC9567 /* TestSetup.swift */,
135-
48BF341C25111194004139AF /* Task.swift */,
136-
48924B892514EAD900CC9567 /* MultipleUsers.swift */,
137136
48F38B4F2527806600DDEB65 /* AccessMongoDB.swift */,
138-
48F38B512527843B00DDEB65 /* CustomUserData.swift */,
139-
48F38B5325278ECB00DDEB65 /* CustomUserData.m */,
140-
48924BBA2515309F00CC9567 /* MultipleUsers.m */,
141137
48E63FB7252646A700F883B1 /* Authenticate.swift */,
142-
48BF34172511095C004139AF /* RealmApp.swift */,
143-
48BF341E251129AD004139AF /* MyRealmApp.m */,
144-
48BF342025112A7F004139AF /* MyRealmApp.h */,
145-
4896EE54251051A600D1FABF /* ExampleSwiftTestCase.swift */,
146-
4896EE57251051C800D1FABF /* ExampleObjcTestCase.m */,
147138
48BF3415251076E4004139AF /* CompleteQuickStart.swift */,
148-
48C072032511B5D5004B02BB /* ManageEmailPasswordUsers.swift */,
149-
48C072052511B6DB004B02BB /* ManageEmailPasswordUsers.m */,
139+
48F38B5325278ECB00DDEB65 /* CustomUserData.m */,
140+
48F38B512527843B00DDEB65 /* CustomUserData.swift */,
141+
4896EE57251051C800D1FABF /* ExampleObjcTestCase.m */,
142+
4896EE54251051A600D1FABF /* ExampleSwiftTestCase.swift */,
143+
48F38B552527926500DDEB65 /* Functions.swift */,
150144
48924B972515008100CC9567 /* Info.plist */,
145+
48C072052511B6DB004B02BB /* ManageEmailPasswordUsers.m */,
146+
48C072032511B5D5004B02BB /* ManageEmailPasswordUsers.swift */,
147+
48924BBA2515309F00CC9567 /* MultipleUsers.m */,
148+
48924B892514EAD900CC9567 /* MultipleUsers.swift */,
149+
48BF342025112A7F004139AF /* MyRealmApp.h */,
150+
48BF341E251129AD004139AF /* MyRealmApp.m */,
151+
48BF34172511095C004139AF /* RealmApp.swift */,
152+
48BF341C25111194004139AF /* Task.swift */,
153+
48924B8B2514FEFC00CC9567 /* TestSetup.swift */,
151154
);
152155
path = Examples;
153156
sourceTree = "<group>";
@@ -362,6 +365,7 @@
362365
48BF3416251076E4004139AF /* CompleteQuickStart.swift in Sources */,
363366
4896EE58251051C800D1FABF /* ExampleObjcTestCase.m in Sources */,
364367
4896EE55251051A600D1FABF /* ExampleSwiftTestCase.swift in Sources */,
368+
48F38B562527926500DDEB65 /* Functions.swift in Sources */,
365369
48924BBB2515309F00CC9567 /* MultipleUsers.m in Sources */,
366370
48F38B5425278ECB00DDEB65 /* CustomUserData.m in Sources */,
367371
48924B8C2514FEFC00CC9567 /* TestSetup.swift in Sources */,
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
let app = App(id: YOUR_REALM_APP_ID)
2+
3+
// ... log in ...
4+
5+
let user = app.currentUser!
6+
7+
// The dynamic member name `sum` is directly associated with the function
8+
// name. The first argument is the `BSONArray` of arguments to be provided
9+
// to the function. The trailing closure is the completion handler
10+
// to call when the function call is complete. This handler is executed on
11+
// a non-main global `DispatchQueue`.
12+
user.functions.sum([1, 2]) { sum, error in
13+
guard error == nil else {
14+
print("Function call failed: \(error!.localizedDescription)")
15+
return
16+
}
17+
guard case let .double(value) = sum else {
18+
print("Unexpected non-double result: \(sum ?? "nil")");
19+
return
20+
}
21+
print("Called function 'sum' and got result: \(value)")
22+
assert(value == 3)
23+
}

source/ios/call-a-function.txt

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -37,26 +37,5 @@ Call from a Client Application
3737
To execute a function from the iOS Client SDK, use the ``functions`` method
3838
of the app.
3939

40-
.. code-block:: swift
41-
42-
let app = App(id: "my-realm-app-id")
43-
// ... log in ...
44-
let user = app.currentUser()!
45-
46-
// The dynamic member name `sum` is directly associated with the function
47-
// name. The first argument is the `BSONArray` of arguments to be provided
48-
// to the function. The trailing closure is the completion handler
49-
// to call when the function call is complete. This handler is executed on
50-
// a non-main global `DispatchQueue`.
51-
user.functions.sum([1, 2]) { sum, error in
52-
guard error == nil else {
53-
print("Function call failed: \(error!.localizedDescription)")
54-
return
55-
}
56-
guard case let .double(value) = sum else {
57-
print("Unexpected non-double result: \(sum ?? "nil")");
58-
return
59-
}
60-
print("Called function: \(value)")
61-
assert(value == 3)
62-
}
40+
.. literalinclude:: /examples/generated/code/start/Functions.codeblock.call-a-function.swift
41+
:language: swift

0 commit comments

Comments
 (0)