-
Notifications
You must be signed in to change notification settings - Fork 90
/
Copy pathWriteExamples.cs
229 lines (197 loc) · 6.63 KB
/
WriteExamples.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
using System;
using System.Collections.Generic;
using System.Linq;
using System.Transactions;
using MongoDB.Bson;
using NUnit.Framework;
using Realms;
using Realms.Sync;
namespace Examples
{
public class Writes
{
Realm realm;
public Writes()
{
realm = Realm.GetInstance();
}
[OneTimeTearDown]
public void TearDown()
{
realm.Write(() =>
{
realm.RemoveAll<WriteDog>();
realm.RemoveAll<WritePerson>();
});
}
[Test]
public void Snippets()
{
realm.Write(() =>
{
// Create someone to take care of some dogs.
var ali = new WritePerson { Id = 44, Name = "Ali" };
realm.Add(ali);
// Find dogs younger than 2.
var puppies = realm.All<WriteDog>().Where(dog => dog.Age < 2);
// Loop through one by one to update.
foreach (var puppy in puppies)
{
// Give all the puppies to Ali.
puppy.Owner = ali;
}
});
var myDog = new WriteDog { Id = 411, Name = "Gracie", Age = 7 };
// :snippet-start: create-long-hand
// :replace-start: {
// "terms": {
// "WritePerson": "Person",
// "WriteDog" : "Dog" }
// }
// Open a thread-safe transaction.
using (var transaction = realm.BeginWrite())
{
// At this point, the TransactionState is "Running":
// transaction.State == TransactionState.Running
try
{
// Perform a write op...
realm.Add(myDog);
// Do other work that needs to be included in
// this transaction
if (transaction.State == TransactionState.Running)
{
transaction.Commit();
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
// Something went wrong; roll back the transaction
if (transaction.State != TransactionState.RolledBack &&
transaction.State != TransactionState.Committed)
{
transaction.Rollback();
}
}
}
// :replace-end:
// :snippet-end:
// :snippet-start: create
// :replace-start: {
// "terms": {
// "WritePerson": "Person",
// "WriteDog" : "Dog" }
// }
// Instantiate a class, as normal.
var dog = new WriteDog { Id = 42, Name = "Max", Age = 5 };
// Open a thread-safe transaction.
realm.Write(() =>
{
// Add the instance to the realm.
realm.Add(dog);
});
// :replace-end:
// :snippet-end:
realm.Write(() =>
{
var drew = new WritePerson { Id = 1234, Name = "Drew" };
// Add a new person to the realm. Since nobody with ID 1234
// has been added yet, this adds the instance to the realm.
realm.Add(drew, update: true);
var andy = new WritePerson { Id = 1234, Name = "Andy" };
// Judging by the ID, it's the same person, just with a different name.
// When `update` is true, you overwrite the original entry (i.e. Drew -> Andy).
realm.Add(andy, update: true);
});
// :snippet-start: modify
// :replace-start: {
// "terms": {
// "dog2": "dog",
// "WriteDog" : "Dog" }
// }
var dog2 = realm.All<WriteDog>().First();
realm.WriteAsync(() =>
{
dog2.Name = "Wolfie";
dog2.Age += 1;
});
// :replace-end:
// :snippet-end:
realm.Write(() =>
{
// Create someone to take care of some dogs.
var ali = new WritePerson { Id = 1, Name = "Ali" };
realm.Add(ali);
// Find dogs younger than 2.
var puppies = realm.All<WriteDog>().Where(dog => dog.Age < 2);
// Loop through one by one to update.
foreach (var puppy in puppies)
{
// Give all the puppies to Ali.
puppy.Owner = ali;
}
});
var foodog = new WriteDog { Id = 123, Name = "FiFi" };
realm.Write(() =>
{
realm.Add(foodog);
});
realm.Write(() =>
{
// Remove the instance from the realm.
realm.Remove(foodog);
// Discard the reference.
foodog = null;
});
realm.Write(() =>
{
// Find dogs younger than 2 years old.
var puppies = realm.All<WriteDog>().Where(dog => dog.Age < 2);
// Remove the collection from the realm.
realm.RemoveRange(puppies);
});
var ali = new WritePerson();
realm.Write(() =>
{
realm.Add(ali);
});
realm.Write(() =>
{
// Remove all of Ali's dogs.
realm.RemoveRange<WriteDog>(ali.Dogs);
// Remove Ali.
realm.Remove(ali);
});
realm.Write(() =>
{
// Remove all instances of Dog from the realm.
realm.RemoveAll<WriteDog>();
});
realm.Write(() =>
{
// Remove all objects from the realm.
realm.RemoveAll();
});
}
}
public class WriteDog : RealmObject
{
[PrimaryKey]
[MapTo("_id")]
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
public string Breed { get; set; }
public WritePerson? Owner { get; set; } = null!;
}
public class WritePerson : RealmObject
{
[PrimaryKey]
[MapTo("_id")]
public int Id { get; set; }
public string Name { get; set; }
[Backlink("Owner")]
public IQueryable<WriteDog> Dogs { get; }
}
}