You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This library is a thin wrapper around __RealmSwift__ ( [Realm Docs](https://realm.io/docs/swift/latest/) ).
9
9
10
-
This library is a thin wrapper around __RealmSwift__.
10
+
**Table of contents:**
11
11
12
-
### Observing collections
12
+
1. Observing object collections
13
+
2. Observing a single object
14
+
3. Write transactions
15
+
4. Automatically binding table and collection views
16
+
5. Example app
13
17
14
-
RxRealm can be used to create `Observable`s from objects of type `Results`, `List`, `LinkingObjects` or `AnyRealmCollection` as follows:
18
+
## Observing object collections
15
19
16
-
#### Observable.from(_:)
17
-
Emits every time the collection changes
20
+
RxRealm can be used to create `Observable`s from objects of type `Results`, `List`, `LinkingObjects` or `AnyRealmCollection`. These types are typically used to load and observe object collections from the Realm Mobile Database.
Fetches the a snapshot of a Realm collection and converts it to an array value (for example if you want to use array methods on the collection)
38
+
The above prints out "X laps" each time a lap is added or removed from the database. If you set `synchronizedStart` to `true` (the default value), the first element will be emitted synchronously - e.g. when you're binding UI you might not be able for an asynchronous notification to come through.
39
+
40
+
##### `Observable.array(from:synchronizedStart:)`
41
+
Upon each change fetches a snapshot of the Realm collection and converts it to an array value (for example if you want to use array methods on the collection):
33
42
34
43
```swift
35
44
let realm =try!Realm()
36
45
let laps = realm.objects(Lap.self)
37
46
38
-
Observable.arrayFrom(laps)
47
+
Observable.array(from: laps)
39
48
.map { array in
40
49
return array.prefix(3) //slice of first 3 items
41
-
}.subscribe(onNext: { text in
50
+
}
51
+
.subscribe(onNext: { text in
42
52
print(text)
43
53
})
44
54
```
45
55
46
-
####Observable.changesetFrom(_:)
47
-
Emits every time the collection changes and provides the exact indexes that has been deleted, inserted or updated
There's a separate API to make it easier to observe single object (it creates `Results` behind the scenes):
102
+
There's a separate API to make it easier to observe a single object (it creates `Results` behind the scenes):
89
103
90
104
```swift
91
-
Observable.from(ticker)
92
-
.map({ (ticker)->Stringin
105
+
Observable.from(object: ticker)
106
+
.map { ticker ->Stringin
93
107
return"\(ticker.ticks) ticks"
94
-
})
108
+
}
95
109
.bindTo(footer.rx.text)
96
110
```
97
111
98
112
This API uses the primary key of the object to query the database for it and observe for change notifications. Observing objects without a primary key does not work.
99
113
100
-
### Performing transactions
114
+
##Write transactions
101
115
102
-
#### rx.add()
103
-
##### **Writing to an existing Realm reference**
116
+
##### `rx.add()`
104
117
105
-
You can add newly created objects to a Realm that you already have initialized:
118
+
Writing objects to **existing** realm reference. You can add newly created objects to a Realm that you already have initialized:
106
119
107
120
```swift
108
121
let realm =try!Realm()
@@ -114,8 +127,9 @@ Observable.from(messages)
114
127
115
128
Be careful, this will retain your Realm until the `Observable` completes or errors out.
116
129
117
-
##### **Writing to the default Realm**
118
-
You can leave it to RxRealm to grab the default Realm on any thread your subscribe and write objects to it:
130
+
##### `Realm.rx.add()`
131
+
132
+
Writing to the default Realm. You can leave it to RxRealm to grab the default Realm on any thread your subscribe and write objects to it:
119
133
120
134
```swift
121
135
let messages = [Message("hello"), Message("world")]
@@ -124,16 +138,17 @@ Observable.from(messages)
124
138
.subscribe(Realm.rx.add())
125
139
```
126
140
127
-
##### **Writing to a specific Realm**
128
-
If you want to switch threads and not use the default Realm, provide a `Realm.Configuration`:
141
+
###### `Realm.rx.add(configuration:)`
142
+
143
+
Writing to a **custom** Realm. If you want to switch threads and not use the default Realm, provide a `Realm.Configuration`:
129
144
130
145
```swift
131
146
var config = Realm.Configuration()
132
147
/* custom configuration settings */
133
148
134
149
let messages = [Message("hello"), Message("world")]
135
150
Observable.from(messages)
136
-
.observeOn( /* you can switch threads if you want to*/ )
151
+
.observeOn( /* you can switch threads here*/ )
137
152
.subscribe(Realm.rx.add(configuration: config))
138
153
```
139
154
@@ -143,7 +158,7 @@ If you want to create a Realm on a different thread manually, allowing you to ha
143
158
let messages = [Message("hello"), Message("world")]
144
159
145
160
Observable.from(messages)
146
-
.observeOn( /* you can switch threads if you want to*/ )
161
+
.observeOn( /* you can switch threads here*/ )
147
162
.subscribe(onNext: {messages in
148
163
let realm =try!Realm()
149
164
try! realm.write {
@@ -152,9 +167,10 @@ Observable.from(messages)
152
167
})
153
168
```
154
169
155
-
#### rx.delete()
170
+
##### `rx.delete()`
171
+
172
+
Deleting object(s) from an existing realm reference:
156
173
157
-
#####**Deleting from an existing realm reference**
158
174
```swift
159
175
let realm =try!Realm()
160
176
let messages = realm.objects(Message.self)
@@ -164,14 +180,46 @@ Observable.from(messages)
164
180
165
181
Be careful, this will retain your realm until the `Observable` completes or errors out.
166
182
167
-
#####**Deleting from the object's realm automatically**
168
-
You can leave it to RxRealm to grab the Realm from the first object and use it:
183
+
##### `Realm.rx.delete()`
184
+
185
+
Deleting from the object's realm automatically. You can leave it to RxRealm to grab the Realm from the first object and use it:
169
186
170
187
```swift
171
188
Observable.from(someCollectionOfPersistedObjects)
172
189
.subscribe(Realm.rx.delete())
173
190
```
174
191
192
+
## Automatically binding table and collection views
193
+
194
+
RxRealm does not depend on UIKit/Cocoa and it doesn't provide built-in way to bind Realm collections to UI components.
195
+
196
+
There is a separate library __`RxRealmDataSources`__[link](https://github.com/RxSwiftCommunity/RxRealmDataSources), which mimics the default data sources library for RxSwift
197
+
198
+
`RxRealmDataSources` allows you to bind directly an observable collection of Realm objects to a table or collection view. Here's how the code to bind a collection of laps to a table view looks like:
199
+
200
+
```swift
201
+
// create data source
202
+
let dataSource = RxTableViewRealmDataSource<Lap>(
203
+
cellIdentifier: "Cell", cellType: PersonCell.self) {cell, ip, lap in
204
+
cell.customLabel.text="\(ip.row). \(lap.text)"
205
+
}
206
+
207
+
// RxRealm to get Observable<Results>
208
+
let realm =try!Realm()
209
+
let lapsList = realm.objects(Timer.self).first!.laps
210
+
let laps = Observable.changeset(from: lapsList)
211
+
212
+
// bind to table view
213
+
laps
214
+
.bindTo(tableView.rx.realmChanges(dataSource))
215
+
.addDisposableTo(bag)
216
+
```
217
+
218
+
The data source will reflect all changes via animations to the table view:
If you want to learn more check the __`RxRealmDataSources`__[README](https://github.com/RxSwiftCommunity/RxRealmDataSources).
175
223
176
224
## Example app
177
225
@@ -195,31 +243,20 @@ pod "RxRealm"
195
243
196
244
#### Carthage
197
245
198
-
RxRealm is available through [Carthage](https://github.com/Carthage/Carthage). You can install Carthage with [Homebrew](http://brew.sh/) using the following command:
199
-
200
-
```bash
201
-
$ brew update
202
-
$ brew install carthage
203
-
```
204
-
205
246
To integrate RxRealm into your Xcode project using Carthage, specify it in your `Cartfile`:
206
247
207
248
```ogdl
208
-
github "RxSwiftCommunity/RxRealm" ~> 1.0
249
+
github "RxSwiftCommunity/RxRealm"
209
250
```
210
251
211
252
Run `carthage update` to build the framework and drag the built `RxRealm.framework` into your Xcode project.
212
253
213
-
#### As Source
214
-
215
-
You can grab the files in `Pod/Classes` from this repo and include them in your project.
216
-
217
254
## TODO
218
255
219
256
* Test add platforms and add compatibility for the pod
220
257
221
258
## License
222
259
223
-
This library belongs to _RxSwiftCommunity_.
260
+
This library belongs to _RxSwiftCommunity_. Maintainer is [Marin Todorov](https://github.com/icanzilb).
224
261
225
262
RxRealm is available under the MIT license. See the LICENSE file for more info.
0 commit comments