mirror of
https://github.com/caoer/RxFirebase.git
synced 2026-01-12 22:52:01 +08:00
firestore
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
use_frameworks!
|
||||
|
||||
target 'RxFirebase_Example' do
|
||||
pod 'RxFirebase', :path => '../'
|
||||
pod 'RxFirebase/Firestore', :path => '../'
|
||||
pod 'RxFirebase/Core', :path => '../'
|
||||
|
||||
target 'RxFirebase_Tests' do
|
||||
inherit! :search_paths
|
||||
@@ -9,3 +10,4 @@ target 'RxFirebase_Example' do
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
92
Example/Podfile.lock
Normal file
92
Example/Podfile.lock
Normal file
@@ -0,0 +1,92 @@
|
||||
PODS:
|
||||
- BoringSSL (10.0):
|
||||
- BoringSSL/Implementation (= 10.0)
|
||||
- BoringSSL/Interface (= 10.0)
|
||||
- BoringSSL/Implementation (10.0):
|
||||
- BoringSSL/Interface (= 10.0)
|
||||
- BoringSSL/Interface (10.0)
|
||||
- FirebaseAnalytics (4.1.0):
|
||||
- FirebaseCore (~> 4.0)
|
||||
- FirebaseInstanceID (~> 2.0)
|
||||
- GoogleToolboxForMac/NSData+zlib (~> 2.1)
|
||||
- nanopb (~> 0.3)
|
||||
- FirebaseCore (4.0.18):
|
||||
- GoogleToolboxForMac/NSData+zlib (~> 2.1)
|
||||
- FirebaseFirestore (0.10.4):
|
||||
- FirebaseAnalytics (~> 4.1)
|
||||
- FirebaseCore (~> 4.0)
|
||||
- gRPC-ProtoRPC (~> 1.0)
|
||||
- leveldb-library (~> 1.18)
|
||||
- Protobuf (~> 3.5)
|
||||
- FirebaseInstanceID (2.0.10):
|
||||
- FirebaseCore (~> 4.0)
|
||||
- GoogleToolboxForMac/Defines (2.1.3)
|
||||
- GoogleToolboxForMac/NSData+zlib (2.1.3):
|
||||
- GoogleToolboxForMac/Defines (= 2.1.3)
|
||||
- gRPC (1.10.0):
|
||||
- gRPC-RxLibrary (= 1.10.0)
|
||||
- gRPC/Main (= 1.10.0)
|
||||
- gRPC-Core (1.10.0):
|
||||
- gRPC-Core/Implementation (= 1.10.0)
|
||||
- gRPC-Core/Interface (= 1.10.0)
|
||||
- gRPC-Core/Implementation (1.10.0):
|
||||
- BoringSSL (~> 10.0)
|
||||
- gRPC-Core/Interface (= 1.10.0)
|
||||
- nanopb (~> 0.3)
|
||||
- gRPC-Core/Interface (1.10.0)
|
||||
- gRPC-ProtoRPC (1.10.0):
|
||||
- gRPC (= 1.10.0)
|
||||
- gRPC-RxLibrary (= 1.10.0)
|
||||
- Protobuf (~> 3.0)
|
||||
- gRPC-RxLibrary (1.10.0)
|
||||
- gRPC/Main (1.10.0):
|
||||
- gRPC-Core (= 1.10.0)
|
||||
- gRPC-RxLibrary (= 1.10.0)
|
||||
- leveldb-library (1.20)
|
||||
- nanopb (0.3.8):
|
||||
- nanopb/decode (= 0.3.8)
|
||||
- nanopb/encode (= 0.3.8)
|
||||
- nanopb/decode (0.3.8)
|
||||
- nanopb/encode (0.3.8)
|
||||
- Protobuf (3.5.0)
|
||||
- RxCocoa (4.1.2):
|
||||
- RxSwift (~> 4.0)
|
||||
- RxFirebase/Core (0.1.0):
|
||||
- FirebaseCore
|
||||
- RxCocoa (~> 4)
|
||||
- RxSwift (~> 4)
|
||||
- RxFirebase/Firestore (0.1.0):
|
||||
- FirebaseFirestore
|
||||
- RxCocoa (~> 4)
|
||||
- RxSwift (~> 4)
|
||||
- RxSwift (4.1.2)
|
||||
|
||||
DEPENDENCIES:
|
||||
- RxFirebase/Core (from `../`)
|
||||
- RxFirebase/Firestore (from `../`)
|
||||
|
||||
EXTERNAL SOURCES:
|
||||
RxFirebase:
|
||||
:path: ../
|
||||
|
||||
SPEC CHECKSUMS:
|
||||
BoringSSL: 32764dbaf5f5888cf51fbaa172a010126b41bcd4
|
||||
FirebaseAnalytics: 3dfae28d4a5e06f86c4fae830efc2ad3fadb19bc
|
||||
FirebaseCore: b981f47e5254cbcfdeb483355300d743f6fcab2c
|
||||
FirebaseFirestore: 796df79c29cf8ecf806564f4d347665bdafa6ef0
|
||||
FirebaseInstanceID: 8d20d890d65c917f9f7d9950b6e10a760ad34321
|
||||
GoogleToolboxForMac: 2501e2ad72a52eb3dfe7bd9aee7dad11b858bd20
|
||||
gRPC: f54f0e6d603052b4562447da442ce2ff30bcdacc
|
||||
gRPC-Core: a030b1678ded49c88ec5ba7c90ee8ee5f47ec6e1
|
||||
gRPC-ProtoRPC: 22712b23eb1bda656a59715fa5c1da0ea1493ea4
|
||||
gRPC-RxLibrary: a41a4652d220f230ba1c0491a94ce2ee04c6180a
|
||||
leveldb-library: 08cba283675b7ed2d99629a4bc5fd052cd2bb6a5
|
||||
nanopb: 5601e6bca2dbf1ed831b519092ec110f66982ca3
|
||||
Protobuf: 8a9838fba8dae3389230e1b7f8c104aa32389c03
|
||||
RxCocoa: d88ba0f1f6abf040011a9eb4b539324fc426843a
|
||||
RxFirebase: 3bc3bbc1e7599c2f3aaedde9debde5537c1d9c1a
|
||||
RxSwift: e49536837d9901277638493ea537394d4b55f570
|
||||
|
||||
PODFILE CHECKSUM: bcea1d407c8677e9d6cda7c19588a1bc242c4f14
|
||||
|
||||
COCOAPODS: 1.4.0
|
||||
@@ -41,10 +41,10 @@
|
||||
607FACEB1AFB9204008FA782 /* Tests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Tests.swift; sourceTree = "<group>"; };
|
||||
6BF2ABCDD7184B5EE57E1330 /* Pods_RxFirebase_Tests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_RxFirebase_Tests.framework; sourceTree = BUILT_PRODUCTS_DIR; };
|
||||
89BF759F7539983B6504973B /* Pods-RxFirebase_Example.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-RxFirebase_Example.release.xcconfig"; path = "Pods/Target Support Files/Pods-RxFirebase_Example/Pods-RxFirebase_Example.release.xcconfig"; sourceTree = "<group>"; };
|
||||
8D1F218AB871F5DC9A8661D7 /* RxFirebase.podspec */ = {isa = PBXFileReference; includeInIndex = 1; name = RxFirebase.podspec; path = ../RxFirebase.podspec; sourceTree = "<group>"; };
|
||||
8D1F218AB871F5DC9A8661D7 /* RxFirebase.podspec */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text; name = RxFirebase.podspec; path = ../RxFirebase.podspec; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.ruby; };
|
||||
9DE96A3C1B64D32C3F85F23E /* Pods-RxFirebase_Tests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-RxFirebase_Tests.release.xcconfig"; path = "Pods/Target Support Files/Pods-RxFirebase_Tests/Pods-RxFirebase_Tests.release.xcconfig"; sourceTree = "<group>"; };
|
||||
A5ECD0739495588E75E2ACBB /* README.md */ = {isa = PBXFileReference; includeInIndex = 1; name = README.md; path = ../README.md; sourceTree = "<group>"; };
|
||||
B2E93982D2C0A28AF5F580D4 /* LICENSE */ = {isa = PBXFileReference; includeInIndex = 1; name = LICENSE; path = ../LICENSE; sourceTree = "<group>"; };
|
||||
A5ECD0739495588E75E2ACBB /* README.md */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = net.daringfireball.markdown; name = README.md; path = ../README.md; sourceTree = "<group>"; };
|
||||
B2E93982D2C0A28AF5F580D4 /* LICENSE */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text; name = LICENSE; path = ../LICENSE; sourceTree = "<group>"; };
|
||||
C6A32035D43AEC5C5947CB67 /* Pods_RxFirebase_Example.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_RxFirebase_Example.framework; sourceTree = BUILT_PRODUCTS_DIR; };
|
||||
D519C2B489975AD7FFBC1420 /* Pods-RxFirebase_Tests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-RxFirebase_Tests.debug.xcconfig"; path = "Pods/Target Support Files/Pods-RxFirebase_Tests/Pods-RxFirebase_Tests.debug.xcconfig"; sourceTree = "<group>"; };
|
||||
/* End PBXFileReference section */
|
||||
@@ -352,11 +352,31 @@
|
||||
);
|
||||
inputPaths = (
|
||||
"${SRCROOT}/Pods/Target Support Files/Pods-RxFirebase_Example/Pods-RxFirebase_Example-frameworks.sh",
|
||||
"${BUILT_PRODUCTS_DIR}/RxFirebase/RxFirebase.framework",
|
||||
"${BUILT_PRODUCTS_DIR}/BoringSSL/openssl.framework",
|
||||
"${BUILT_PRODUCTS_DIR}/GoogleToolboxForMac/GoogleToolboxForMac.framework",
|
||||
"${BUILT_PRODUCTS_DIR}/Protobuf/Protobuf.framework",
|
||||
"${BUILT_PRODUCTS_DIR}/RxCocoa/RxCocoa.framework",
|
||||
"${BUILT_PRODUCTS_DIR}/RxSwift/RxSwift.framework",
|
||||
"${BUILT_PRODUCTS_DIR}/gRPC/GRPCClient.framework",
|
||||
"${BUILT_PRODUCTS_DIR}/gRPC-Core/grpc.framework",
|
||||
"${BUILT_PRODUCTS_DIR}/gRPC-ProtoRPC/ProtoRPC.framework",
|
||||
"${BUILT_PRODUCTS_DIR}/gRPC-RxLibrary/RxLibrary.framework",
|
||||
"${BUILT_PRODUCTS_DIR}/leveldb-library/leveldb.framework",
|
||||
"${BUILT_PRODUCTS_DIR}/nanopb/nanopb.framework",
|
||||
);
|
||||
name = "[CP] Embed Pods Frameworks";
|
||||
outputPaths = (
|
||||
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/RxFirebase.framework",
|
||||
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/openssl.framework",
|
||||
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/GoogleToolboxForMac.framework",
|
||||
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/Protobuf.framework",
|
||||
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/RxCocoa.framework",
|
||||
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/RxSwift.framework",
|
||||
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/GRPCClient.framework",
|
||||
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/grpc.framework",
|
||||
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/ProtoRPC.framework",
|
||||
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/RxLibrary.framework",
|
||||
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/leveldb.framework",
|
||||
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/nanopb.framework",
|
||||
);
|
||||
runOnlyForDeploymentPostprocessing = 0;
|
||||
shellPath = /bin/sh;
|
||||
|
||||
10
Example/RxFirebase.xcworkspace/contents.xcworkspacedata
generated
Normal file
10
Example/RxFirebase.xcworkspace/contents.xcworkspacedata
generated
Normal file
@@ -0,0 +1,10 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Workspace
|
||||
version = "1.0">
|
||||
<FileRef
|
||||
location = "group:RxFirebase.xcodeproj">
|
||||
</FileRef>
|
||||
<FileRef
|
||||
location = "group:Pods/Pods.xcodeproj">
|
||||
</FileRef>
|
||||
</Workspace>
|
||||
2
LICENSE
2
LICENSE
@@ -1,4 +1,4 @@
|
||||
Copyright (c) 2018 Arnaud Dorgans <ad@lanoosphere.com>
|
||||
Copyright (c) 2018 Arnaud Dorgans <arnaud.dorgans@gmail.com>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
|
||||
@@ -22,7 +22,7 @@ pod 'RxFirebase'
|
||||
|
||||
## Author
|
||||
|
||||
Arnaud Dorgans, ad@lanoosphere.com
|
||||
Arnaud Dorgans, arnaud.dorgans@gmail.com
|
||||
|
||||
## License
|
||||
|
||||
|
||||
@@ -24,19 +24,27 @@ TODO: Add long description of the pod here.
|
||||
s.homepage = 'https://github.com/Arnaud Dorgans/RxFirebase'
|
||||
# s.screenshots = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'
|
||||
s.license = { :type => 'MIT', :file => 'LICENSE' }
|
||||
s.author = { 'Arnaud Dorgans' => 'ad@lanoosphere.com' }
|
||||
s.author = { 'Arnaud Dorgans' => 'arnaud.dorgans@gmail.com' }
|
||||
s.source = { :git => 'https://github.com/Arnaud Dorgans/RxFirebase.git', :tag => s.version.to_s }
|
||||
# s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>'
|
||||
|
||||
s.ios.deployment_target = '8.0'
|
||||
s.default_subspec = 'Core'
|
||||
s.static_framework = true
|
||||
s.dependency 'RxSwift', '~> 4'
|
||||
s.dependency 'RxCocoa', '~> 4'
|
||||
s.dependency 'FirebaseCore', '~> 4'
|
||||
|
||||
s.source_files = 'RxFirebase/Classes/**/*'
|
||||
s.subspec 'Core' do |core|
|
||||
core.source_files = 'RxFirebase/Classes/Core/**/*'
|
||||
end
|
||||
s.subspec 'Database' do |database|
|
||||
database.source_files = 'RxFirebase/Classes/Database/**/*'
|
||||
database.dependency 'FirebaseDatabase', '~> 4'
|
||||
end
|
||||
s.subspec 'Firestore' do |firestore|
|
||||
firestore.source_files = 'RxFirebase/Classes/Firestore/**/*'
|
||||
firestore.dependency 'FirebaseFirestore', '~> 0'
|
||||
end
|
||||
|
||||
# s.resource_bundles = {
|
||||
# 'RxFirebase' => ['RxFirebase/Assets/*.png']
|
||||
# }
|
||||
|
||||
# s.public_header_files = 'Pod/Classes/**/*.h'
|
||||
# s.frameworks = 'UIKit', 'MapKit'
|
||||
# s.dependency 'AFNetworking', '~> 2.3'
|
||||
end
|
||||
|
||||
40
RxFirebase/Classes/Firestore/FIRCollectionReference+Rx.swift
Normal file
40
RxFirebase/Classes/Firestore/FIRCollectionReference+Rx.swift
Normal file
@@ -0,0 +1,40 @@
|
||||
//
|
||||
// FIRCollectionReference+Rx.swift
|
||||
// RxFirebase
|
||||
//
|
||||
// Created by Arnaud Dorgans on 31/03/2018.
|
||||
//
|
||||
|
||||
import RxCocoa
|
||||
import RxSwift
|
||||
import FirebaseFirestore
|
||||
|
||||
extension Reactive where Base: CollectionReference {
|
||||
|
||||
/**
|
||||
* Add a new document to this collection with the specified data, assigning it a document ID
|
||||
* automatically.
|
||||
*
|
||||
* @param data An `NSDictionary` containing the data for the new document.
|
||||
* @param completion A block to execute once the document has been successfully written to
|
||||
* the server. This block will not be called while the client is offline, though local
|
||||
* changes will be visible immediately.
|
||||
*
|
||||
* @return A `FIRDocumentReference` pointing to the newly created document.
|
||||
*/
|
||||
public func addDocument(data: [String: Any]) -> Observable<DocumentReference> {
|
||||
return Observable<DocumentReference>.create { observer in
|
||||
var ref: DocumentReference?
|
||||
let _ref = self.base.addDocument(data: data) { error in
|
||||
if let error = error {
|
||||
observer.onError(error)
|
||||
} else if let ref = ref {
|
||||
observer.onNext(ref)
|
||||
}
|
||||
observer.onCompleted()
|
||||
}
|
||||
ref = _ref
|
||||
return Disposables.create()
|
||||
}
|
||||
}
|
||||
}
|
||||
153
RxFirebase/Classes/Firestore/FIRDocumentReference+Rx.swift
Normal file
153
RxFirebase/Classes/Firestore/FIRDocumentReference+Rx.swift
Normal file
@@ -0,0 +1,153 @@
|
||||
//
|
||||
// FIRDocumentReference+Rx.swift
|
||||
// RxFirebase
|
||||
//
|
||||
// Created by Arnaud Dorgans on 31/03/2018.
|
||||
//
|
||||
|
||||
import RxCocoa
|
||||
import RxSwift
|
||||
import FirebaseFirestore
|
||||
|
||||
extension Reactive where Base: DocumentReference {
|
||||
|
||||
/**
|
||||
* Overwrites the document referred to by this `FIRDocumentReference`. If no document exists, it
|
||||
* is created. If a document already exists, it is overwritten.
|
||||
*
|
||||
* @param documentData An `NSDictionary` containing the fields that make up the document
|
||||
* to be written.
|
||||
* @param completion A block to execute once the document has been successfully written to the
|
||||
* server. This block will not be called while the client is offline, though local
|
||||
* changes will be visible immediately.
|
||||
*/
|
||||
public func setData(_ documentData: [String: Any]) -> Observable<Void> {
|
||||
return Observable<Void>.create { observer in
|
||||
self.base.setData(documentData) { error in
|
||||
guard let error = error else {
|
||||
observer.onNext(())
|
||||
observer.onCompleted()
|
||||
return
|
||||
}
|
||||
observer.onError(error)
|
||||
}
|
||||
return Disposables.create()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes to the document referred to by this DocumentReference. If the document does not yet
|
||||
* exist, it will be created. If you pass `FIRSetOptions`, the provided data will be merged into
|
||||
* an existing document.
|
||||
*
|
||||
* @param documentData An `NSDictionary` containing the fields that make up the document
|
||||
* to be written.
|
||||
* @param options A `FIRSetOptions` used to configure the set behavior.
|
||||
* @param completion A block to execute once the document has been successfully written to the
|
||||
* server. This block will not be called while the client is offline, though local
|
||||
* changes will be visible immediately.
|
||||
*/
|
||||
public func setData(_ documentData: [String: Any], options: SetOptions) -> Observable<Void> {
|
||||
return Observable<Void>.create { observer in
|
||||
self.base.setData(documentData, options: options) { error in
|
||||
guard let error = error else {
|
||||
observer.onNext(())
|
||||
observer.onCompleted()
|
||||
return
|
||||
}
|
||||
observer.onError(error)
|
||||
}
|
||||
return Disposables.create()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates fields in the document referred to by this `FIRDocumentReference`. If the document
|
||||
* does not exist, the update fails and the specified completion block receives an error.
|
||||
*
|
||||
* @param fields An `NSDictionary` containing the fields (expressed as an `NSString` or
|
||||
* `FIRFieldPath`) and values with which to update the document.
|
||||
* @param completion A block to execute when the update is complete. If the update is successful the
|
||||
* error parameter will be nil, otherwise it will give an indication of how the update failed.
|
||||
* This block will only execute when the client is online and the commit has completed against
|
||||
* the server. The completion handler will not be called when the device is offline, though
|
||||
* local changes will be visible immediately.
|
||||
*/
|
||||
public func updateData(_ fields: [AnyHashable: Any]) -> Observable<Void> {
|
||||
return Observable<Void>.create { observer in
|
||||
self.base.updateData(fields) { error in
|
||||
guard let error = error else {
|
||||
observer.onNext(())
|
||||
observer.onCompleted()
|
||||
return
|
||||
}
|
||||
observer.onError(error)
|
||||
}
|
||||
return Disposables.create()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes the document referred to by this `FIRDocumentReference`.
|
||||
*
|
||||
* @param completion A block to execute once the document has been successfully written to the
|
||||
* server. This block will not be called while the client is offline, though local
|
||||
* changes will be visible immediately.
|
||||
*/
|
||||
public func delete() -> Observable<Void> {
|
||||
return Observable<Void>.create { observer in
|
||||
self.base.delete { error in
|
||||
guard let error = error else {
|
||||
observer.onNext(())
|
||||
observer.onCompleted()
|
||||
return
|
||||
}
|
||||
observer.onError(error)
|
||||
}
|
||||
return Disposables.create()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads the document referenced by this `FIRDocumentReference`.
|
||||
*
|
||||
* @param completion a block to execute once the document has been successfully read.
|
||||
*/
|
||||
public func getDocument() -> Observable<DocumentSnapshot> {
|
||||
return Observable<DocumentSnapshot>.create { observer in
|
||||
self.base.getDocument { snapshot, error in
|
||||
if let error = error {
|
||||
observer.onError(error)
|
||||
} else if let snapshot = snapshot {
|
||||
observer.onNext(snapshot)
|
||||
}
|
||||
observer.onCompleted()
|
||||
}
|
||||
return Disposables.create()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Attaches a listener for DocumentSnapshot events.
|
||||
*
|
||||
* @param options Options controlling the listener behavior.
|
||||
* @param listener The listener to attach.
|
||||
*
|
||||
* @return A FIRListenerRegistration that can be used to remove this listener.
|
||||
*/
|
||||
public func addSnapshotListener(options: DocumentListenOptions? = nil) -> Observable<DocumentSnapshot> {
|
||||
return Observable<DocumentSnapshot>.create { observer in
|
||||
let listener = self.base.addSnapshotListener(options: options) { snapshot, error in
|
||||
if let error = error {
|
||||
observer.onError(error)
|
||||
} else if let snapshot = snapshot {
|
||||
observer.onNext(snapshot)
|
||||
}
|
||||
observer.onCompleted()
|
||||
}
|
||||
return Disposables.create {
|
||||
listener.remove()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
94
RxFirebase/Classes/Firestore/FIRFirestore+Rx.swift
Normal file
94
RxFirebase/Classes/Firestore/FIRFirestore+Rx.swift
Normal file
@@ -0,0 +1,94 @@
|
||||
//
|
||||
// FIRFirestore+Rx.swift
|
||||
// RxFirebase
|
||||
//
|
||||
// Created by Arnaud Dorgans on 31/03/2018.
|
||||
//
|
||||
|
||||
import RxCocoa
|
||||
import RxSwift
|
||||
import FirebaseFirestore
|
||||
|
||||
extension Reactive where Base: Firestore {
|
||||
|
||||
/**
|
||||
* Disables usage of the network by this Firestore instance. It can be re-enabled by via
|
||||
* `enableNetworkWithCompletion`. While the network is disabled, any snapshot listeners or get calls
|
||||
* will return results from cache and any write operations will be queued until the network is
|
||||
* restored. The completion block, if provided, will be called once network usage has been disabled.
|
||||
*/
|
||||
func disableNetwork() -> Observable<Void> {
|
||||
return Observable.create { observer in
|
||||
self.base.disableNetwork(completion: { error in
|
||||
guard let error = error else {
|
||||
observer.onNext(())
|
||||
observer.onCompleted()
|
||||
return
|
||||
}
|
||||
observer.onError(error)
|
||||
})
|
||||
return Disposables.create()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Re-enables usage of the network by this Firestore instance after a prior call to
|
||||
* `disableNetworkWithCompletion`. Completion block, if provided, will be called once network uasge
|
||||
* has been enabled.
|
||||
*/
|
||||
func enableNetwork() -> Observable<Void> {
|
||||
return Observable.create { observer in
|
||||
self.base.enableNetwork(completion: { error in
|
||||
guard let error = error else {
|
||||
observer.onNext(())
|
||||
observer.onCompleted()
|
||||
return
|
||||
}
|
||||
observer.onError(error)
|
||||
})
|
||||
return Disposables.create()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes the given updateBlock and then attempts to commit the changes applied within an atomic
|
||||
* transaction.
|
||||
*
|
||||
* In the updateBlock, a set of reads and writes can be performed atomically using the
|
||||
* `FIRTransaction` object passed to the block. After the updateBlock is run, Firestore will attempt
|
||||
* to apply the changes to the server. If any of the data read has been modified outside of this
|
||||
* transaction since being read, then the transaction will be retried by executing the updateBlock
|
||||
* again. If the transaction still fails after 5 retries, then the transaction will fail.
|
||||
*
|
||||
* Since the updateBlock may be executed multiple times, it should avoiding doing anything that
|
||||
* would cause side effects.
|
||||
*
|
||||
* Any value maybe be returned from the updateBlock. If the transaction is successfully committed,
|
||||
* then the completion block will be passed that value. The updateBlock also has an `NSError` out
|
||||
* parameter. If this is set, then the transaction will not attempt to commit, and the given error
|
||||
* will be passed to the completion block.
|
||||
*
|
||||
* The `FIRTransaction` object passed to the updateBlock contains methods for accessing documents
|
||||
* and collections. Unlike other firestore access, data accessed with the transaction will not
|
||||
* reflect local changes that have not been committed. For this reason, it is required that all
|
||||
* reads are performed before any writes. Transactions must be performed while online. Otherwise,
|
||||
* reads will fail, the final commit will fail, and the completion block will return an error.
|
||||
*
|
||||
* @param updateBlock The block to execute within the transaction context.
|
||||
* @param completion The block to call with the result or error of the transaction. This
|
||||
* block will run even if the client is offline, unless the process is killed.
|
||||
*/
|
||||
func runTransaction(_ updateBlock: @escaping (Transaction, NSErrorPointer)->Any?) -> Observable<Any> {
|
||||
return Observable.create { observer in
|
||||
self.base.runTransaction(updateBlock) { value, error in
|
||||
if let error = error {
|
||||
observer.onError(error)
|
||||
} else if let value = value {
|
||||
observer.onNext(value)
|
||||
}
|
||||
observer.onCompleted()
|
||||
}
|
||||
return Disposables.create()
|
||||
}
|
||||
}
|
||||
}
|
||||
57
RxFirebase/Classes/Firestore/FIRQuery+Rx.swift
Normal file
57
RxFirebase/Classes/Firestore/FIRQuery+Rx.swift
Normal file
@@ -0,0 +1,57 @@
|
||||
//
|
||||
// FIRQuery+Rx.swift
|
||||
// RxFirebase
|
||||
//
|
||||
// Created by Arnaud Dorgans on 31/03/2018.
|
||||
//
|
||||
|
||||
import RxCocoa
|
||||
import RxSwift
|
||||
import FirebaseFirestore
|
||||
|
||||
extension Reactive where Base: Query {
|
||||
|
||||
/**
|
||||
* Reads the documents matching this query.
|
||||
*
|
||||
* @param completion a block to execute once the documents have been successfully read.
|
||||
* documentSet will be `nil` only if error is `non-nil`.
|
||||
*/
|
||||
public func getDocuments() -> Observable<QuerySnapshot> {
|
||||
return Observable<QuerySnapshot>.create { observer in
|
||||
self.base.getDocuments { snapshot, error in
|
||||
if let error = error {
|
||||
observer.onError(error)
|
||||
} else if let snapshot = snapshot {
|
||||
observer.onNext(snapshot)
|
||||
}
|
||||
observer.onCompleted()
|
||||
}
|
||||
return Disposables.create()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Attaches a listener for QuerySnapshot events.
|
||||
*
|
||||
* @param options Options controlling the listener behavior.
|
||||
* @param listener The listener to attach.
|
||||
*
|
||||
* @return A FIRListenerRegistration that can be used to remove this listener.
|
||||
*/
|
||||
public func addSnapshotListener(options: QueryListenOptions? = nil) -> Observable<QuerySnapshot> {
|
||||
return Observable<QuerySnapshot>.create { observer in
|
||||
let listener = self.base.addSnapshotListener(options: options) { snapshot, error in
|
||||
if let error = error {
|
||||
observer.onError(error)
|
||||
} else if let snapshot = snapshot {
|
||||
observer.onNext(snapshot)
|
||||
}
|
||||
observer.onCompleted()
|
||||
}
|
||||
return Disposables.create {
|
||||
listener.remove()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
36
RxFirebase/Classes/Firestore/FIRWriteBatch+Rx.swift
Normal file
36
RxFirebase/Classes/Firestore/FIRWriteBatch+Rx.swift
Normal file
@@ -0,0 +1,36 @@
|
||||
//
|
||||
// FIRWriteBatch+Rx.swift
|
||||
// RxFirebase
|
||||
//
|
||||
// Created by Arnaud Dorgans on 31/03/2018.
|
||||
//
|
||||
|
||||
import RxCocoa
|
||||
import RxSwift
|
||||
import FirebaseFirestore
|
||||
|
||||
extension Reactive where Base: WriteBatch {
|
||||
|
||||
/**
|
||||
* Commits all of the writes in this write batch as a single atomic unit.
|
||||
*
|
||||
* @param completion A block to be called once all of the writes in the batch have been
|
||||
* successfully written to the backend as an atomic unit. This block will only execute
|
||||
* when the client is online and the commit has completed against the server. The
|
||||
* completion handler will not be called when the device is offline, though local
|
||||
* changes will be visible immediately.
|
||||
*/
|
||||
public func commit() -> Observable<Void> {
|
||||
return Observable.create { observer in
|
||||
self.base.commit { error in
|
||||
guard let error = error else {
|
||||
observer.onNext(())
|
||||
observer.onCompleted()
|
||||
return
|
||||
}
|
||||
observer.onError(error)
|
||||
}
|
||||
return Disposables.create()
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user