Skip to content

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 10 commits into from
Oct 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@
'guides': ('https://docs.mongodb.com/guides%s', ''),
'java-sdk': ('https://docs.mongodb.com/realm-sdks/java/10.0.0-BETA.8/%s', ''),
'kotlin-sdk': ('https://docs.mongodb.com/realm-sdks/kotlin/10.0.0-BETA.8/%s', ''),
'swift-sdk': ('https://docs.mongodb.com/realm-sdks/swift/10.0.0-beta.6/%s', ''),
'objc-sdk': ('https://docs.mongodb.com/realm-sdks/objc/10.0.0-beta.6/%s', ''),
'swift-sdk': ('https://docs.mongodb.com/realm-sdks/swift/10.0.0-rc.1/%s', ''),
'objc-sdk': ('https://docs.mongodb.com/realm-sdks/objc/10.0.0-rc.1/%s', ''),
'js-sdk': ('https://docs.mongodb.com/realm-sdks/js/latest/%s', ''),
# True External Links
'android': ('https://developer.android.com/%s', ''),
Expand Down
50 changes: 50 additions & 0 deletions examples/ios/Examples/AccessMongoDB.swift
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
Copy link
Contributor

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..."

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)
}

}
30 changes: 30 additions & 0 deletions examples/ios/Examples/AnonymouslyLoggedInTestCase.swift
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)
}


}
36 changes: 12 additions & 24 deletions examples/ios/Examples/Authenticate.swift
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

Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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!)")
Expand All @@ -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 {
Expand All @@ -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!)")
Expand All @@ -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 {
Expand All @@ -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()
Expand Down
10 changes: 7 additions & 3 deletions examples/ios/Examples/CompleteQuickStart.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class CompleteQuickStartTest: XCTestCase {

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

func testRunExample() {
let expectation = XCTestExpectation(description: "Run complete quick start")
// :code-block-start: quick-start
// Now logged in, do something with user
let user = app.currentUser()
let user = app.currentUser
let partitionValue = "myPartition"

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

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

app.currentUser()?.logOut() { (error) in
app.currentUser?.logOut() { (error) in
// Logged out or error occurred
}

// Invalidate notification tokens when done observing
notificationToken.invalidate()
// :hide-start:
expectation.fulfill()
// :hide-end:
}
// :code-block-end:
wait(for: [expectation], timeout: 10.0)
}
}
113 changes: 113 additions & 0 deletions examples/ios/Examples/CustomUserData.m
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
Loading