aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar Sebastian Schmidt <mrschmidt@google.com>2017-12-15 05:35:23 +0800
committerGravatar GitHub <noreply@github.com>2017-12-15 05:35:23 +0800
commit98e08bc0b883d24cf2a0e658924ddd14dbf07d65 (patch)
tree00284647e4d0fd02c645d62e48a5b73b618dc934
parent8f85bd199d2e3060605d2cede6409cedd7538cf5 (diff)
parent08bd327bbfd352ecac783528300c55f1d738e4ae (diff)
Merge pull request #565 from firebase/mrschmidt-missingmethods
Adding missing SnapshotOptions method
-rw-r--r--Firestore/Example/SwiftBuildTest/main.swift25
-rw-r--r--Firestore/Source/API/FIRDocumentSnapshot.m6
-rw-r--r--Firestore/Source/Public/FIRDocumentSnapshot.h14
3 files changed, 38 insertions, 7 deletions
diff --git a/Firestore/Example/SwiftBuildTest/main.swift b/Firestore/Example/SwiftBuildTest/main.swift
index 475c1b2..c05f378 100644
--- a/Firestore/Example/SwiftBuildTest/main.swift
+++ b/Firestore/Example/SwiftBuildTest/main.swift
@@ -167,11 +167,20 @@ func readDocument(at docRef: DocumentReference) {
// Trailing closure syntax.
docRef.getDocument() { document, error in
if let document = document {
- // NOTE that document is nullable.
- let data = document.data();
+ // Note that both document and document.data() is nullable.
+ if let data = document.data() {
print("Read document: \(data)")
-
- // Fields are read via subscript notation.
+ }
+ if let data = document.data(with:SnapshotOptions.serverTimestampBehavior(.estimate)) {
+ print("Read document: \(data)")
+ }
+ if let foo = document.get("foo") {
+ print("Field: \(foo)")
+ }
+ if let foo = document.get("foo", options: SnapshotOptions.serverTimestampBehavior(.previous)) {
+ print("Field: \(foo)")
+ }
+ // Fields can also be read via subscript notation.
if let foo = document["foo"] {
print("Field: \(foo)")
}
@@ -214,7 +223,9 @@ func listenToDocument(at docRef: DocumentReference) {
}
if let document = document {
- print("Current document: \(document.data())");
+ // Note that document.data() is nullable.
+ let data : [String:Any]? = document.data()
+ print("Current document: \(data)");
if (document.metadata.isFromCache) {
print("From Cache")
} else {
@@ -241,7 +252,9 @@ func listenToDocuments(matching query: Query) {
// TODO(mikelehen): Figure out how to make "for..in" syntax work
// directly on documentSet.
for document in snap.documents {
- print("Doc: ", document.data())
+ // Note that document.data() is not nullable.
+ let data : [String:Any] = document.data()
+ print("Doc: ", data)
}
}
}
diff --git a/Firestore/Source/API/FIRDocumentSnapshot.m b/Firestore/Source/API/FIRDocumentSnapshot.m
index 36430ac..65c7b61 100644
--- a/Firestore/Source/API/FIRDocumentSnapshot.m
+++ b/Firestore/Source/API/FIRDocumentSnapshot.m
@@ -213,6 +213,12 @@ NS_ASSUME_NONNULL_BEGIN
return data;
}
+- (NSDictionary<NSString *, id> *)dataWithOptions:(FIRSnapshotOptions *)options {
+ NSDictionary<NSString *, id> *data = [super dataWithOptions:options];
+ FSTAssert(data, @"Document in a QueryDocumentSnapshot should exist");
+ return data;
+}
+
@end
NS_ASSUME_NONNULL_END
diff --git a/Firestore/Source/Public/FIRDocumentSnapshot.h b/Firestore/Source/Public/FIRDocumentSnapshot.h
index a628834..6e79a7f 100644
--- a/Firestore/Source/Public/FIRDocumentSnapshot.h
+++ b/Firestore/Source/Public/FIRDocumentSnapshot.h
@@ -45,7 +45,7 @@ typedef NS_ENUM(NSInteger, FIRServerTimestampBehavior) {
* have not yet been set to their final value.
*/
FIRServerTimestampBehaviorPrevious
-};
+} NS_SWIFT_NAME(ServerTimestampBehavior);
/**
* Options that configure how data is retrieved from a `DocumentSnapshot`
@@ -180,10 +180,22 @@ NS_SWIFT_NAME(QueryDocumentSnapshot)
/**
* Retrieves all fields in the document as an `NSDictionary`.
*
+ * Server-provided timestamps that have not yet been set to their final value will be returned as
+ * `NSNull`. You can use `dataWithOptions()` to configure this behavior.
+ *
* @return An `NSDictionary` containing all fields in the document.
*/
- (NSDictionary<NSString *, id> *)data;
+/**
+ * Retrieves all fields in the document as a `Dictionary`.
+ *
+ * @param options `SnapshotOptions` to configure how data is returned from the snapshot (e.g. the
+ * desired behavior for server timestamps that have not yet been set to their final value).
+ * @return A `Dictionary` containing all fields in the document.
+ */
+- (NSDictionary<NSString *, id> *)dataWithOptions:(FIRSnapshotOptions *)options;
+
@end
NS_ASSUME_NONNULL_END