FIRCollectionReference.mm 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Copyright 2017 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import "FIRCollectionReference.h"
  17. #include <utility>
  18. #import "Firestore/Source/API/FIRDocumentReference+Internal.h"
  19. #import "Firestore/Source/API/FIRFirestore+Internal.h"
  20. #import "Firestore/Source/API/FIRQuery+Internal.h"
  21. #import "Firestore/Source/API/FSTUserDataConverter.h"
  22. #include "Firestore/core/src/firebase/firestore/api/collection_reference.h"
  23. #include "Firestore/core/src/firebase/firestore/model/resource_path.h"
  24. #include "Firestore/core/src/firebase/firestore/util/error_apple.h"
  25. #include "Firestore/core/src/firebase/firestore/util/exception.h"
  26. #include "Firestore/core/src/firebase/firestore/util/string_apple.h"
  27. namespace util = firebase::firestore::util;
  28. using firebase::firestore::api::CollectionReference;
  29. using firebase::firestore::api::DocumentReference;
  30. using firebase::firestore::core::ParsedSetData;
  31. using firebase::firestore::model::ResourcePath;
  32. using firebase::firestore::util::ThrowInvalidArgument;
  33. NS_ASSUME_NONNULL_BEGIN
  34. @implementation FIRCollectionReference
  35. - (instancetype)initWithReference:(CollectionReference &&)reference {
  36. return [super initWithQuery:std::move(reference)];
  37. }
  38. - (instancetype)initWithPath:(ResourcePath)path
  39. firestore:(std::shared_ptr<api::Firestore>)firestore {
  40. CollectionReference ref(std::move(path), std::move(firestore));
  41. return [self initWithReference:std::move(ref)];
  42. }
  43. // Override the designated initializer from the super class.
  44. - (instancetype)initWithQuery:(api::Query &&)query {
  45. HARD_FAIL("Use FIRCollectionReference initWithPath: initializer.");
  46. }
  47. // NSObject Methods
  48. - (BOOL)isEqual:(nullable id)other {
  49. if (other == self) return YES;
  50. if (![[other class] isEqual:[self class]]) return NO;
  51. return [self isEqualToReference:other];
  52. }
  53. - (BOOL)isEqualToReference:(nullable FIRCollectionReference *)otherReference {
  54. if (self == otherReference) return YES;
  55. if (otherReference == nil) return NO;
  56. return self.reference == otherReference.reference;
  57. }
  58. - (NSUInteger)hash {
  59. return self.reference.Hash();
  60. }
  61. - (const CollectionReference &)reference {
  62. // TODO(wilhuff): Use some alternate method for doing this.
  63. //
  64. // Casting from Query& to CollectionReference& when the value is actually a
  65. // Query violates aliasing rules and is technically undefined behavior.
  66. // Nevertheless this works on Clang so this is good enough for now.
  67. return static_cast<const CollectionReference &>(self.apiQuery);
  68. }
  69. - (NSString *)collectionID {
  70. return util::MakeNSString(self.reference.collection_id());
  71. }
  72. - (FIRDocumentReference *_Nullable)parent {
  73. absl::optional<DocumentReference> parent = self.reference.parent();
  74. if (!parent) {
  75. return nil;
  76. }
  77. return [[FIRDocumentReference alloc] initWithReference:std::move(*parent)];
  78. }
  79. - (NSString *)path {
  80. return util::MakeNSString(self.reference.path());
  81. }
  82. - (FIRDocumentReference *)documentWithPath:(NSString *)documentPath {
  83. if (!documentPath) {
  84. ThrowInvalidArgument("Document path cannot be nil.");
  85. }
  86. DocumentReference child = self.reference.Document(util::MakeString(documentPath));
  87. return [[FIRDocumentReference alloc] initWithReference:std::move(child)];
  88. }
  89. - (FIRDocumentReference *)addDocumentWithData:(NSDictionary<NSString *, id> *)data {
  90. return [self addDocumentWithData:data completion:nil];
  91. }
  92. - (FIRDocumentReference *)addDocumentWithData:(NSDictionary<NSString *, id> *)data
  93. completion:
  94. (nullable void (^)(NSError *_Nullable error))completion {
  95. ParsedSetData parsed = [self.firestore.dataConverter parsedSetData:data];
  96. DocumentReference docRef =
  97. self.reference.AddDocument(std::move(parsed), util::MakeCallback(completion));
  98. return [[FIRDocumentReference alloc] initWithReference:std::move(docRef)];
  99. }
  100. - (FIRDocumentReference *)documentWithAutoID {
  101. return [[FIRDocumentReference alloc] initWithReference:self.reference.Document()];
  102. }
  103. @end
  104. NS_ASSUME_NONNULL_END