Skip to content

Commit 726aed2

Browse files
authored
(DOCSP-14648): Error handling (#997)
* (DOCSP-14648): Error handling * Re-add missing on-this-page
1 parent ade7740 commit 726aed2

File tree

5 files changed

+78
-0
lines changed

5 files changed

+78
-0
lines changed

examples/ios/Examples/OpenCloseRealm.m

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,19 @@ - (void)testConfigureObjectTypes {
7373
(void)realm;
7474
}
7575

76+
- (void)testHandleError {
77+
// :code-block-start: handle-error
78+
NSError *error = nil;
79+
RLMRealmConfiguration *config = [RLMRealmConfiguration defaultConfiguration];
80+
RLMRealm *realm = [RLMRealm realmWithConfiguration:config error:&error];
81+
if (!realm) {
82+
// Handle error
83+
return;
84+
}
85+
// Use realm
86+
// :code-block-end:
87+
}
88+
7689
@end
7790

7891
// :replace-end:

examples/ios/Examples/OpenCloseRealm.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,15 @@ class OpenCloseRealm: XCTestCase {
5252
// :code-block-end:
5353
print(fileUrl)
5454
}
55+
56+
func testHandleError() {
57+
// :code-block-start: handle-error
58+
do {
59+
let realm = try Realm()
60+
// Use realm
61+
} catch let error as NSError {
62+
// Handle error
63+
}
64+
// :code-block-end:
65+
}
5566
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
NSError *error = nil;
2+
RLMRealmConfiguration *config = [RLMRealmConfiguration defaultConfiguration];
3+
RLMRealm *realm = [RLMRealm realmWithConfiguration:config error:&error];
4+
if (!realm) {
5+
// Handle error
6+
return;
7+
}
8+
// Use realm
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
do {
2+
let realm = try Realm()
3+
// Use realm
4+
} catch let error as NSError {
5+
// Handle error
6+
}

source/sdk/ios/examples/configure-and-open-a-realm.txt

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@
44
Configure & Open a Realm - iOS SDK
55
==================================
66

7+
.. default-domain:: mongodb
8+
9+
.. contents:: On this page
10+
:local:
11+
:backlinks: none
12+
:depth: 2
13+
:class: singlecol
14+
715
A **{+realm+}** is the core data structure used to organize data in
816
{+client-database+}. For more information, see :ref:`ios-realms`.
917

@@ -67,6 +75,32 @@ When a {+realm+} goes out of scope and is removed from memory due to
6775
<https://docs.swift.org/swift-book/LanguageGuide/AutomaticReferenceCounting.html>`__,
6876
the {+realm+} is closed.
6977

78+
.. _ios-handle-errors:
79+
80+
Handle Errors
81+
-------------
82+
83+
.. tabs-realm-languages::
84+
85+
.. tab::
86+
:tabid: swift
87+
88+
To handle errors when accessing a {+realm+}, use Swift's built-in
89+
error handling mechanism:
90+
91+
.. literalinclude:: /examples/generated/code/start/OpenCloseRealm.codeblock.handle-error.swift
92+
:language: swift
93+
94+
.. tab::
95+
:tabid: objective-c
96+
97+
To handle errors when accessing a {+realm+}, provide an
98+
``NSError`` pointer to the ``error`` parameter:
99+
100+
.. literalinclude:: /examples/generated/code/start/OpenCloseRealm.codeblock.handle-error.m
101+
:language: objectivec
102+
103+
70104
.. _ios-open-an-in-memory-realm:
71105

72106
Open an In-Memory Realm
@@ -102,6 +136,12 @@ closed.
102136
.. literalinclude:: /examples/generated/code/start/OpenCloseRealm.codeblock.open-in-memory-realm.m
103137
:language: objectivec
104138

139+
.. important::
140+
141+
When all *in-memory* {+realm+} instances with a particular identifier
142+
go out of scope, {+client-database+} deletes **all data** in that
143+
{+realm+}. To avoid this, hold onto a strong reference to any
144+
in-memory {+realms+} during your app's lifetime.
105145

106146
.. _ios-provide-a-subset-of-classes-to-a-realm:
107147

0 commit comments

Comments
 (0)