-
Notifications
You must be signed in to change notification settings - Fork 88
iOS RC 1 #511
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
iOS RC 1 #511
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
a180e60
Update for iOS rc 1
cbush 8a08e84
Update code examples for iOS rc 1
cbush b084f9a
Update remote MongoDB example
cbush 8476df1
Update custom user data examples
cbush 93703c2
Update call a function example
cbush e32b972
Update Manage API Keys examples
cbush ffb2d31
Update multiple users examples
cbush 7e664e2
Remove Credentials table
cbush 388c3ce
Update example links
cbush 6c6f672
Address comments
cbush File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import XCTest | ||
import RealmSwift | ||
|
||
class AccessMongoDB: AnonymouslyLoggedInTestCase { | ||
func testRemoteMongoDB() { | ||
let expectation = XCTestExpectation(description: "it completes") | ||
|
||
// :code-block-start: remote-mongodb | ||
// mongodb-atlas is the cluster service name | ||
let client = app.currentUser!.mongoClient("mongodb-atlas") | ||
|
||
// Select the database | ||
let database = client.database(named: "tracker") | ||
|
||
// Select the collection | ||
let collection = database.collection(withName: "Task") | ||
|
||
// Using the user's id to look up tasks | ||
let user = app.currentUser! | ||
let identity = user.id | ||
|
||
// Run the query | ||
collection.find(filter: ["_partition": AnyBSON(identity)], { (results, error) in | ||
// Note: this completion handler may be called on a background thread. | ||
// If you intend to operate on the UI, dispatch back to the main | ||
// thread with `DispatchQueue.main.sync {}`. | ||
|
||
// Handle errors | ||
guard error == nil else { | ||
print("Call to MongoDB failed: \(error!.localizedDescription)") | ||
// :hide-start: | ||
XCTAssertEqual(error!.localizedDescription, "no rule exists for namespace 'tracker.Task'") | ||
expectation.fulfill() | ||
// :hide-end: | ||
return | ||
} | ||
// Print each document | ||
print("Results:") | ||
results!.forEach({(document) in | ||
print("Document:") | ||
document.forEach({ (key, value) in | ||
print(" key: \(key), value: \(value)") | ||
}) | ||
}) | ||
}) | ||
// :code-block-end: | ||
wait(for: [expectation], timeout: 10) | ||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import XCTest | ||
import RealmSwift | ||
|
||
// Derive from this class whenever you need to use | ||
// app.currentUser with an existing anonymous login. | ||
class AnonymouslyLoggedInTestCase: XCTestCase { | ||
override func setUp() { | ||
let expectation = XCTestExpectation(description: "logs in") | ||
app.login(credentials: Credentials.anonymous) { (user, error) in | ||
guard error == nil else { | ||
fatalError("Login failed: \(error!.localizedDescription)") | ||
} | ||
expectation.fulfill() | ||
} | ||
wait(for: [expectation], timeout: 10) | ||
} | ||
|
||
override func tearDown() { | ||
let expectation = XCTestExpectation(description: "logs out") | ||
app.currentUser!.remove { (error) in | ||
guard error == nil else { | ||
fatalError("Failed to log out: \(error!.localizedDescription)") | ||
} | ||
expectation.fulfill() | ||
} | ||
wait(for: [expectation], timeout: 10) | ||
} | ||
|
||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,3 @@ | ||
// | ||
// Authenticate.swift | ||
// RealmExamples | ||
// | ||
// Created by Chris Bush on 2020-10-01. | ||
// Copyright © 2020 MongoDB, Inc. All rights reserved. | ||
// | ||
|
||
import XCTest | ||
import RealmSwift | ||
|
||
|
@@ -15,7 +7,7 @@ class Authenticate: XCTestCase { | |
|
||
// :code-block-start: google | ||
// Fetch Google token via the Google SDK | ||
let credentials = Credentials(googleAuthCode: "<token>") | ||
let credentials = Credentials.google(serverAuthCode: "<token>") | ||
app.login(credentials: credentials) { (user, error) in | ||
DispatchQueue.main.sync { | ||
guard error == nil else { | ||
|
@@ -38,7 +30,7 @@ class Authenticate: XCTestCase { | |
|
||
// :code-block-start: apple | ||
// Fetch Apple token via the Apple SDK | ||
let credentials = Credentials(appleToken: "<token>") | ||
let credentials = Credentials.apple(idToken: "<token>") | ||
app.login(credentials: credentials) { (user, error) in | ||
DispatchQueue.main.sync { | ||
guard error == nil else { | ||
|
@@ -61,7 +53,7 @@ class Authenticate: XCTestCase { | |
|
||
// :code-block-start: facebook | ||
// Fetch Facebook token via the Facebook SDK | ||
let credentials = Credentials(facebookToken: "<token>") | ||
let credentials = Credentials.facebook(accessToken: "<token>") | ||
app.login(credentials: credentials) { (user, error) in | ||
DispatchQueue.main.sync { | ||
guard error == nil else { | ||
|
@@ -83,7 +75,7 @@ class Authenticate: XCTestCase { | |
let expectation = XCTestExpectation(description: "login completes") | ||
|
||
// :code-block-start: jwt | ||
let credentials = Credentials(jwt: "<jwt>") | ||
let credentials = Credentials.jwt(token: "<jwt>") | ||
app.login(credentials: credentials) { (user, error) in | ||
DispatchQueue.main.sync { | ||
guard error == nil else { | ||
|
@@ -105,13 +97,9 @@ class Authenticate: XCTestCase { | |
let expectation = XCTestExpectation(description: "login completes") | ||
|
||
// :code-block-start: function | ||
let params = [ | ||
"username": "bob" | ||
] | ||
|
||
var e: NSError? | ||
|
||
app.login(credentials: Credentials(functionPayload: params, error: &e)) { (user, error) in | ||
let params: Document = ["username": "bob"] | ||
|
||
app.login(credentials: Credentials.function(payload: params)) { (user, error) in | ||
DispatchQueue.main.sync { | ||
guard error == nil else { | ||
print("Login failed: \(error!)") | ||
|
@@ -131,7 +119,7 @@ class Authenticate: XCTestCase { | |
let expectation = XCTestExpectation(description: "login completes") | ||
|
||
// :code-block-start: api-key | ||
let credentials = Credentials(userAPIKey: "<api-key>") | ||
let credentials = Credentials.userAPIKey("<api-key>") | ||
app.login(credentials: credentials) { (user, error) in | ||
DispatchQueue.main.sync { | ||
guard error == nil else { | ||
|
@@ -155,7 +143,7 @@ class Authenticate: XCTestCase { | |
// :code-block-start: email-password | ||
let email = "[email protected]" | ||
let password = "12345" | ||
app.login(credentials: Credentials(email: email, password: password)) { (user, error) in | ||
app.login(credentials: Credentials.emailPassword(email: email, password: password)) { (user, error) in | ||
DispatchQueue.main.sync { | ||
guard error == nil else { | ||
print("Login failed: \(error!)") | ||
|
@@ -176,7 +164,7 @@ class Authenticate: XCTestCase { | |
let expectation = XCTestExpectation(description: "login completes") | ||
|
||
// :code-block-start: anonymous | ||
let anonymousCredentials = Credentials.anonymous() | ||
let anonymousCredentials = Credentials.anonymous | ||
app.login(credentials: anonymousCredentials) { (user, error) in | ||
DispatchQueue.main.sync { | ||
guard error == nil else { | ||
|
@@ -195,12 +183,12 @@ class Authenticate: XCTestCase { | |
} | ||
|
||
override func tearDown() { | ||
guard app.currentUser() != nil else { | ||
guard app.currentUser != nil else { | ||
return | ||
} | ||
let expectation = XCTestExpectation(description: "logout completes") | ||
// :code-block-start: logout | ||
app.currentUser()?.logOut { (error) in | ||
app.currentUser?.logOut { (error) in | ||
// user is logged out or there was an error | ||
// :hide-start: | ||
expectation.fulfill() | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
#import <XCTest/XCTest.h> | ||
#import <Realm/Realm.h> | ||
#import "MyRealmApp.h" | ||
|
||
@interface CustomUserData : XCTestCase | ||
|
||
@end | ||
|
||
@implementation CustomUserData | ||
|
||
- (void)testCreateCustomUserData { | ||
XCTestExpectation *expectation = [self expectationWithDescription:@"it completes"]; | ||
|
||
// :code-block-start: create-custom-user-data | ||
RLMApp *app = [RLMApp appWithId:YOUR_REALM_APP_ID]; | ||
[app loginWithCredential:[RLMCredentials anonymousCredentials] completion:^(RLMUser *user, NSError *error) { | ||
if (error != nil) { | ||
NSLog(@"Failed to log in: %@", error); | ||
return; | ||
} | ||
RLMMongoClient *client = [user mongoClientWithServiceName:@"mongodb-atlas"]; | ||
RLMMongoDatabase *database = [client databaseWithName:@"my_database"]; | ||
RLMMongoCollection *collection = [database collectionWithName:@"users"]; | ||
[collection insertOneDocument: | ||
@{@"userId": [user identifier], @"favoriteColor": @"pink"} | ||
completion:^(RLMObjectId *newObjectId, NSError *error) { | ||
if (error != nil) { | ||
NSLog(@"Failed to insert: %@", error); | ||
// :hide-start: | ||
XCTAssertEqualObjects([error localizedDescription], @"no rule exists for namespace 'my_database.users'"); | ||
[expectation fulfill]; | ||
// :hide-end: | ||
} | ||
NSLog(@"Inserted custom user data document with object ID: %@", newObjectId); | ||
}]; | ||
}]; | ||
// :code-block-end: | ||
|
||
[self waitForExpectationsWithTimeout:10 handler:^(NSError *error) { | ||
NSLog(@"Expectation failed: %@", error); | ||
}]; | ||
} | ||
|
||
- (void)testReadCustomUserData { | ||
XCTestExpectation *expectation = [self expectationWithDescription:@"it completes"]; | ||
|
||
// :code-block-start: read-custom-user-data | ||
RLMApp *app = [RLMApp appWithId:YOUR_REALM_APP_ID]; | ||
[app loginWithCredential:[RLMCredentials anonymousCredentials] completion:^(RLMUser *user, NSError *error) { | ||
if (error != nil) { | ||
NSLog(@"Failed to log in: %@", error); | ||
return; | ||
} | ||
|
||
// If the user data has been refreshed recently, you can access the | ||
// custom user data directly on the user object | ||
NSLog(@"User custom data: %@", [user customData]); | ||
|
||
// Refresh the custom data | ||
[user refreshCustomDataWithCompletion:^(NSDictionary *customData, NSError *error) { | ||
if (error != nil) { | ||
NSLog(@"Failed to refresh custom user data: %@", error); | ||
return; | ||
} | ||
NSLog(@"Favorite color: %@", customData[@"favoriteColor"]); | ||
// :hide-start: | ||
[expectation fulfill]; | ||
// :hide-end: | ||
}]; | ||
}]; | ||
// :code-block-end: | ||
|
||
[self waitForExpectationsWithTimeout:10 handler:^(NSError *error) { | ||
NSLog(@"Expectation failed: %@", error); | ||
}]; | ||
} | ||
|
||
- (void)testUpdateCustomUserData { | ||
XCTestExpectation *expectation = [self expectationWithDescription:@"it completes"]; | ||
|
||
// :code-block-start: update-custom-user-data | ||
RLMApp *app = [RLMApp appWithId:YOUR_REALM_APP_ID]; | ||
[app loginWithCredential:[RLMCredentials anonymousCredentials] completion:^(RLMUser *user, NSError *error) { | ||
if (error != nil) { | ||
NSLog(@"Failed to log in: %@", error); | ||
return; | ||
} | ||
RLMMongoClient *client = [user mongoClientWithServiceName:@"mongodb-atlas"]; | ||
RLMMongoDatabase *database = [client databaseWithName:@"my_database"]; | ||
RLMMongoCollection *collection = [database collectionWithName:@"users"]; | ||
|
||
// Update the user's custom data document | ||
[collection updateOneDocumentWhere:@{@"userId": [user identifier]} | ||
updateDocument: @{@"favoriteColor": @"cerulean"} | ||
completion:^(RLMUpdateResult *updateResult, NSError *error) { | ||
if (error != nil) { | ||
NSLog(@"Failed to insert: %@", error); | ||
// :hide-start: | ||
XCTAssertEqualObjects([error localizedDescription], @"no rule exists for namespace 'my_database.users'"); | ||
[expectation fulfill]; | ||
// :hide-end: | ||
} | ||
NSLog(@"Matched: %lu, modified: %lu", [updateResult matchedCount], [updateResult modifiedCount]); | ||
}]; | ||
}]; | ||
// :code-block-end: | ||
|
||
[self waitForExpectationsWithTimeout:10 handler:^(NSError *error) { | ||
NSLog(@"Expectation failed: %@", error); | ||
}]; | ||
} | ||
|
||
@end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nitpick: if you want to match the imperative form of the other comments, you can use: "Use the user's id..."