This commit is contained in:
Arnaud Dorgans
2018-05-16 12:16:33 +02:00
parent 823f4bdb86
commit 10ddddd307
5 changed files with 111 additions and 6 deletions

View File

@@ -23,6 +23,7 @@ it, simply add the following line to your Podfile:
```ruby
pod 'RxFirebase/Firestore'
pod 'RxFirebase/RemoteConfig'
pod 'RxFirebase/Database'
```
## Usage
@@ -31,6 +32,92 @@ pod 'RxFirebase/RemoteConfig'
import RxFirebase
```
### Database
Basic write operation:
```swift
let ref = Database.database().reference()
ref.child("users")
.child("1")
.rx
.setValue(["username": "Arnonymous"])
.subscribe(onNext: { _ in
print("Document successfully updated")
}).disposed(by: disposeBag)
// https://firebase.google.com/docs/database/ios/read-and-write#basic_write
```
Listen for value events:
```swift
let ref = Database.database().reference()
ref.child("users")
.child("1")
.rx
.observeEvent(.value)
.subscribe(onNext: { snapshot in
print("Value:\(snapshot.value)")
}).disposed(by: disposeBag)
// https://firebase.google.com/docs/database/ios/read-and-write#listen_for_value_events
```
Read data once:
```swift
let ref = Database.database().reference()
ref.child("users")
.child("1")
.rx
.observeSingleEvent(.value)
.subscribe(onNext: { snapshot in
print("Value:\(snapshot.value)")
}).disposed(by: disposeBag)
// https://firebase.google.com/docs/database/ios/read-and-write#read_data_once
```
Update specific fields:
```swift
let ref = Database.database().reference()
let childUpdates = ["/posts/\(key)": post,
"/user-posts/\(userID)/\(key)/": post]
ref.rx.updateChildValues(childUpdates)
.subscribe(onNext: { _ in
// Success
}).disposed(by: disposeBag)
// https://firebase.google.com/docs/database/ios/read-and-write#update_specific_fields
```
Delete data:
```swift
let ref = Database.database().reference()
ref.rx.removeValue()
.subscribe(onNext: { _ in
// Success
}).disposed(by: disposeBag)
// https://firebase.google.com/docs/database/ios/read-and-write#delete_data
```
Save data as transactions
```swift
let ref = Database.database().reference()
ref.rx.runTransactionBlock { currentData in
// TransactionResult
}.subscribe(onNext: { _ in
// Success
}).disposed(by: disposeBag)
// https://firebase.google.com/docs/database/ios/read-and-write#save_data_as_transactions
```
### Firestore
Setting data: