Skip to content

Commit 29ed063

Browse files
authored
iOS RC 1 (#511)
* Update for iOS rc 1 * Update code examples for iOS rc 1 * Update remote MongoDB example * Update custom user data examples * Update call a function example * Update Manage API Keys examples * Update multiple users examples * Remove Credentials table - This was getting out of sync - The complete examples now show everything, so it is no longer needed * Update example links * Address comments
1 parent 326c4ba commit 29ed063

File tree

69 files changed

+1446
-476
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+1446
-476
lines changed

conf.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@
106106
'guides': ('https://docs.mongodb.com/guides%s', ''),
107107
'java-sdk': ('https://docs.mongodb.com/realm-sdks/java/10.0.0-BETA.8/%s', ''),
108108
'kotlin-sdk': ('https://docs.mongodb.com/realm-sdks/kotlin/10.0.0-BETA.8/%s', ''),
109-
'swift-sdk': ('https://docs.mongodb.com/realm-sdks/swift/10.0.0-beta.6/%s', ''),
110-
'objc-sdk': ('https://docs.mongodb.com/realm-sdks/objc/10.0.0-beta.6/%s', ''),
109+
'swift-sdk': ('https://docs.mongodb.com/realm-sdks/swift/10.0.0-rc.1/%s', ''),
110+
'objc-sdk': ('https://docs.mongodb.com/realm-sdks/objc/10.0.0-rc.1/%s', ''),
111111
'js-sdk': ('https://docs.mongodb.com/realm-sdks/js/latest/%s', ''),
112112
# True External Links
113113
'android': ('https://developer.android.com/%s', ''),
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import XCTest
2+
import RealmSwift
3+
4+
class AccessMongoDB: AnonymouslyLoggedInTestCase {
5+
func testRemoteMongoDB() {
6+
let expectation = XCTestExpectation(description: "it completes")
7+
8+
// :code-block-start: remote-mongodb
9+
// mongodb-atlas is the cluster service name
10+
let client = app.currentUser!.mongoClient("mongodb-atlas")
11+
12+
// Select the database
13+
let database = client.database(named: "tracker")
14+
15+
// Select the collection
16+
let collection = database.collection(withName: "Task")
17+
18+
// Using the user's id to look up tasks
19+
let user = app.currentUser!
20+
let identity = user.id
21+
22+
// Run the query
23+
collection.find(filter: ["_partition": AnyBSON(identity)], { (results, error) in
24+
// Note: this completion handler may be called on a background thread.
25+
// If you intend to operate on the UI, dispatch back to the main
26+
// thread with `DispatchQueue.main.sync {}`.
27+
28+
// Handle errors
29+
guard error == nil else {
30+
print("Call to MongoDB failed: \(error!.localizedDescription)")
31+
// :hide-start:
32+
XCTAssertEqual(error!.localizedDescription, "no rule exists for namespace 'tracker.Task'")
33+
expectation.fulfill()
34+
// :hide-end:
35+
return
36+
}
37+
// Print each document
38+
print("Results:")
39+
results!.forEach({(document) in
40+
print("Document:")
41+
document.forEach({ (key, value) in
42+
print(" key: \(key), value: \(value)")
43+
})
44+
})
45+
})
46+
// :code-block-end:
47+
wait(for: [expectation], timeout: 10)
48+
}
49+
50+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import XCTest
2+
import RealmSwift
3+
4+
// Derive from this class whenever you need to use
5+
// app.currentUser with an existing anonymous login.
6+
class AnonymouslyLoggedInTestCase: XCTestCase {
7+
override func setUp() {
8+
let expectation = XCTestExpectation(description: "logs in")
9+
app.login(credentials: Credentials.anonymous) { (user, error) in
10+
guard error == nil else {
11+
fatalError("Login failed: \(error!.localizedDescription)")
12+
}
13+
expectation.fulfill()
14+
}
15+
wait(for: [expectation], timeout: 10)
16+
}
17+
18+
override func tearDown() {
19+
let expectation = XCTestExpectation(description: "logs out")
20+
app.currentUser!.remove { (error) in
21+
guard error == nil else {
22+
fatalError("Failed to log out: \(error!.localizedDescription)")
23+
}
24+
expectation.fulfill()
25+
}
26+
wait(for: [expectation], timeout: 10)
27+
}
28+
29+
30+
}

examples/ios/Examples/Authenticate.swift

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,3 @@
1-
//
2-
// Authenticate.swift
3-
// RealmExamples
4-
//
5-
// Created by Chris Bush on 2020-10-01.
6-
// Copyright © 2020 MongoDB, Inc. All rights reserved.
7-
//
8-
91
import XCTest
102
import RealmSwift
113

@@ -15,7 +7,7 @@ class Authenticate: XCTestCase {
157

168
// :code-block-start: google
179
// Fetch Google token via the Google SDK
18-
let credentials = Credentials(googleAuthCode: "<token>")
10+
let credentials = Credentials.google(serverAuthCode: "<token>")
1911
app.login(credentials: credentials) { (user, error) in
2012
DispatchQueue.main.sync {
2113
guard error == nil else {
@@ -38,7 +30,7 @@ class Authenticate: XCTestCase {
3830

3931
// :code-block-start: apple
4032
// Fetch Apple token via the Apple SDK
41-
let credentials = Credentials(appleToken: "<token>")
33+
let credentials = Credentials.apple(idToken: "<token>")
4234
app.login(credentials: credentials) { (user, error) in
4335
DispatchQueue.main.sync {
4436
guard error == nil else {
@@ -61,7 +53,7 @@ class Authenticate: XCTestCase {
6153

6254
// :code-block-start: facebook
6355
// Fetch Facebook token via the Facebook SDK
64-
let credentials = Credentials(facebookToken: "<token>")
56+
let credentials = Credentials.facebook(accessToken: "<token>")
6557
app.login(credentials: credentials) { (user, error) in
6658
DispatchQueue.main.sync {
6759
guard error == nil else {
@@ -83,7 +75,7 @@ class Authenticate: XCTestCase {
8375
let expectation = XCTestExpectation(description: "login completes")
8476

8577
// :code-block-start: jwt
86-
let credentials = Credentials(jwt: "<jwt>")
78+
let credentials = Credentials.jwt(token: "<jwt>")
8779
app.login(credentials: credentials) { (user, error) in
8880
DispatchQueue.main.sync {
8981
guard error == nil else {
@@ -105,13 +97,9 @@ class Authenticate: XCTestCase {
10597
let expectation = XCTestExpectation(description: "login completes")
10698

10799
// :code-block-start: function
108-
let params = [
109-
"username": "bob"
110-
]
111-
112-
var e: NSError?
113-
114-
app.login(credentials: Credentials(functionPayload: params, error: &e)) { (user, error) in
100+
let params: Document = ["username": "bob"]
101+
102+
app.login(credentials: Credentials.function(payload: params)) { (user, error) in
115103
DispatchQueue.main.sync {
116104
guard error == nil else {
117105
print("Login failed: \(error!)")
@@ -131,7 +119,7 @@ class Authenticate: XCTestCase {
131119
let expectation = XCTestExpectation(description: "login completes")
132120

133121
// :code-block-start: api-key
134-
let credentials = Credentials(userAPIKey: "<api-key>")
122+
let credentials = Credentials.userAPIKey("<api-key>")
135123
app.login(credentials: credentials) { (user, error) in
136124
DispatchQueue.main.sync {
137125
guard error == nil else {
@@ -155,7 +143,7 @@ class Authenticate: XCTestCase {
155143
// :code-block-start: email-password
156144
let email = "[email protected]"
157145
let password = "12345"
158-
app.login(credentials: Credentials(email: email, password: password)) { (user, error) in
146+
app.login(credentials: Credentials.emailPassword(email: email, password: password)) { (user, error) in
159147
DispatchQueue.main.sync {
160148
guard error == nil else {
161149
print("Login failed: \(error!)")
@@ -176,7 +164,7 @@ class Authenticate: XCTestCase {
176164
let expectation = XCTestExpectation(description: "login completes")
177165

178166
// :code-block-start: anonymous
179-
let anonymousCredentials = Credentials.anonymous()
167+
let anonymousCredentials = Credentials.anonymous
180168
app.login(credentials: anonymousCredentials) { (user, error) in
181169
DispatchQueue.main.sync {
182170
guard error == nil else {
@@ -195,12 +183,12 @@ class Authenticate: XCTestCase {
195183
}
196184

197185
override func tearDown() {
198-
guard app.currentUser() != nil else {
186+
guard app.currentUser != nil else {
199187
return
200188
}
201189
let expectation = XCTestExpectation(description: "logout completes")
202190
// :code-block-start: logout
203-
app.currentUser()?.logOut { (error) in
191+
app.currentUser?.logOut { (error) in
204192
// user is logged out or there was an error
205193
// :hide-start:
206194
expectation.fulfill()

examples/ios/Examples/CompleteQuickStart.swift

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class CompleteQuickStartTest: XCTestCase {
3131

3232
override func setUp() {
3333
let expectation = XCTestExpectation(description: "Log in successfully")
34-
app.login(credentials: Credentials.anonymous()) { (user, error) in
34+
app.login(credentials: Credentials.anonymous) { (user, error) in
3535
// Remember to dispatch back to the main thread in completion handlers
3636
// if you want to do anything on the UI.
3737
DispatchQueue.main.async {
@@ -58,8 +58,9 @@ class CompleteQuickStartTest: XCTestCase {
5858

5959
func testRunExample() {
6060
let expectation = XCTestExpectation(description: "Run complete quick start")
61+
// :code-block-start: quick-start
6162
// Now logged in, do something with user
62-
let user = app.currentUser()
63+
let user = app.currentUser
6364
let partitionValue = "myPartition"
6465

6566
var configuration = user!.configuration(partitionValue: partitionValue)
@@ -120,14 +121,17 @@ class CompleteQuickStartTest: XCTestCase {
120121

121122
print("A list of all tasks after deleting one: \(tasks)")
122123

123-
app.currentUser()?.logOut() { (error) in
124+
app.currentUser?.logOut() { (error) in
124125
// Logged out or error occurred
125126
}
126127

127128
// Invalidate notification tokens when done observing
128129
notificationToken.invalidate()
130+
// :hide-start:
129131
expectation.fulfill()
132+
// :hide-end:
130133
}
134+
// :code-block-end:
131135
wait(for: [expectation], timeout: 10.0)
132136
}
133137
}
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
#import <XCTest/XCTest.h>
2+
#import <Realm/Realm.h>
3+
#import "MyRealmApp.h"
4+
5+
@interface CustomUserData : XCTestCase
6+
7+
@end
8+
9+
@implementation CustomUserData
10+
11+
- (void)testCreateCustomUserData {
12+
XCTestExpectation *expectation = [self expectationWithDescription:@"it completes"];
13+
14+
// :code-block-start: create-custom-user-data
15+
RLMApp *app = [RLMApp appWithId:YOUR_REALM_APP_ID];
16+
[app loginWithCredential:[RLMCredentials anonymousCredentials] completion:^(RLMUser *user, NSError *error) {
17+
if (error != nil) {
18+
NSLog(@"Failed to log in: %@", error);
19+
return;
20+
}
21+
RLMMongoClient *client = [user mongoClientWithServiceName:@"mongodb-atlas"];
22+
RLMMongoDatabase *database = [client databaseWithName:@"my_database"];
23+
RLMMongoCollection *collection = [database collectionWithName:@"users"];
24+
[collection insertOneDocument:
25+
@{@"userId": [user identifier], @"favoriteColor": @"pink"}
26+
completion:^(RLMObjectId *newObjectId, NSError *error) {
27+
if (error != nil) {
28+
NSLog(@"Failed to insert: %@", error);
29+
// :hide-start:
30+
XCTAssertEqualObjects([error localizedDescription], @"no rule exists for namespace 'my_database.users'");
31+
[expectation fulfill];
32+
// :hide-end:
33+
}
34+
NSLog(@"Inserted custom user data document with object ID: %@", newObjectId);
35+
}];
36+
}];
37+
// :code-block-end:
38+
39+
[self waitForExpectationsWithTimeout:10 handler:^(NSError *error) {
40+
NSLog(@"Expectation failed: %@", error);
41+
}];
42+
}
43+
44+
- (void)testReadCustomUserData {
45+
XCTestExpectation *expectation = [self expectationWithDescription:@"it completes"];
46+
47+
// :code-block-start: read-custom-user-data
48+
RLMApp *app = [RLMApp appWithId:YOUR_REALM_APP_ID];
49+
[app loginWithCredential:[RLMCredentials anonymousCredentials] completion:^(RLMUser *user, NSError *error) {
50+
if (error != nil) {
51+
NSLog(@"Failed to log in: %@", error);
52+
return;
53+
}
54+
55+
// If the user data has been refreshed recently, you can access the
56+
// custom user data directly on the user object
57+
NSLog(@"User custom data: %@", [user customData]);
58+
59+
// Refresh the custom data
60+
[user refreshCustomDataWithCompletion:^(NSDictionary *customData, NSError *error) {
61+
if (error != nil) {
62+
NSLog(@"Failed to refresh custom user data: %@", error);
63+
return;
64+
}
65+
NSLog(@"Favorite color: %@", customData[@"favoriteColor"]);
66+
// :hide-start:
67+
[expectation fulfill];
68+
// :hide-end:
69+
}];
70+
}];
71+
// :code-block-end:
72+
73+
[self waitForExpectationsWithTimeout:10 handler:^(NSError *error) {
74+
NSLog(@"Expectation failed: %@", error);
75+
}];
76+
}
77+
78+
- (void)testUpdateCustomUserData {
79+
XCTestExpectation *expectation = [self expectationWithDescription:@"it completes"];
80+
81+
// :code-block-start: update-custom-user-data
82+
RLMApp *app = [RLMApp appWithId:YOUR_REALM_APP_ID];
83+
[app loginWithCredential:[RLMCredentials anonymousCredentials] completion:^(RLMUser *user, NSError *error) {
84+
if (error != nil) {
85+
NSLog(@"Failed to log in: %@", error);
86+
return;
87+
}
88+
RLMMongoClient *client = [user mongoClientWithServiceName:@"mongodb-atlas"];
89+
RLMMongoDatabase *database = [client databaseWithName:@"my_database"];
90+
RLMMongoCollection *collection = [database collectionWithName:@"users"];
91+
92+
// Update the user's custom data document
93+
[collection updateOneDocumentWhere:@{@"userId": [user identifier]}
94+
updateDocument: @{@"favoriteColor": @"cerulean"}
95+
completion:^(RLMUpdateResult *updateResult, NSError *error) {
96+
if (error != nil) {
97+
NSLog(@"Failed to insert: %@", error);
98+
// :hide-start:
99+
XCTAssertEqualObjects([error localizedDescription], @"no rule exists for namespace 'my_database.users'");
100+
[expectation fulfill];
101+
// :hide-end:
102+
}
103+
NSLog(@"Matched: %lu, modified: %lu", [updateResult matchedCount], [updateResult modifiedCount]);
104+
}];
105+
}];
106+
// :code-block-end:
107+
108+
[self waitForExpectationsWithTimeout:10 handler:^(NSError *error) {
109+
NSLog(@"Expectation failed: %@", error);
110+
}];
111+
}
112+
113+
@end

0 commit comments

Comments
 (0)