FIRCollectionReference.mm 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. #import "FIRFirestore.h"
  18. #include "Firestore/core/src/firebase/firestore/util/autoid.h"
  19. #import "Firestore/Source/API/FIRDocumentReference+Internal.h"
  20. #import "Firestore/Source/API/FIRQuery+Internal.h"
  21. #import "Firestore/Source/API/FIRQuery_Init.h"
  22. #import "Firestore/Source/Core/FSTQuery.h"
  23. #import "Firestore/Source/Util/FSTUsageValidation.h"
  24. #include "Firestore/core/src/firebase/firestore/model/document_key.h"
  25. #include "Firestore/core/src/firebase/firestore/model/resource_path.h"
  26. #include "Firestore/core/src/firebase/firestore/util/string_apple.h"
  27. namespace util = firebase::firestore::util;
  28. using firebase::firestore::model::DocumentKey;
  29. using firebase::firestore::model::ResourcePath;
  30. using firebase::firestore::util::CreateAutoId;
  31. NS_ASSUME_NONNULL_BEGIN
  32. @interface FIRCollectionReference ()
  33. - (instancetype)initWithPath:(const ResourcePath &)path
  34. firestore:(FIRFirestore *)firestore NS_DESIGNATED_INITIALIZER;
  35. // Mark the super class designated initializer unavailable.
  36. - (instancetype)initWithQuery:(FSTQuery *)query
  37. firestore:(FIRFirestore *)firestore
  38. __attribute__((unavailable("Use the initWithPath constructor of FIRCollectionReference.")));
  39. @end
  40. @implementation FIRCollectionReference (Internal)
  41. + (instancetype)referenceWithPath:(const ResourcePath &)path firestore:(FIRFirestore *)firestore {
  42. return [[FIRCollectionReference alloc] initWithPath:path firestore:firestore];
  43. }
  44. @end
  45. @implementation FIRCollectionReference
  46. - (instancetype)initWithPath:(const ResourcePath &)path firestore:(FIRFirestore *)firestore {
  47. if (path.size() % 2 != 1) {
  48. FSTThrowInvalidArgument(@"Invalid collection reference. Collection references must have an odd "
  49. "number of segments, but %s has %zu",
  50. path.CanonicalString().c_str(), path.size());
  51. }
  52. self = [super initWithQuery:[FSTQuery queryWithPath:path] firestore:firestore];
  53. return self;
  54. }
  55. // Override the designated initializer from the super class.
  56. - (instancetype)initWithQuery:(FSTQuery *)query firestore:(FIRFirestore *)firestore {
  57. HARD_FAIL("Use FIRCollectionReference initWithPath: initializer.");
  58. }
  59. // NSObject Methods
  60. - (BOOL)isEqual:(nullable id)other {
  61. if (other == self) return YES;
  62. if (![[other class] isEqual:[self class]]) return NO;
  63. return [self isEqualToReference:other];
  64. }
  65. - (BOOL)isEqualToReference:(nullable FIRCollectionReference *)reference {
  66. if (self == reference) return YES;
  67. if (reference == nil) return NO;
  68. return [self.firestore isEqual:reference.firestore] && [self.query isEqual:reference.query];
  69. }
  70. - (NSUInteger)hash {
  71. NSUInteger hash = [self.firestore hash];
  72. hash = hash * 31u + [self.query hash];
  73. return hash;
  74. }
  75. - (NSString *)collectionID {
  76. return util::WrapNSString(self.query.path.last_segment());
  77. }
  78. - (FIRDocumentReference *_Nullable)parent {
  79. const ResourcePath parentPath = self.query.path.PopLast();
  80. if (parentPath.empty()) {
  81. return nil;
  82. } else {
  83. DocumentKey key{parentPath};
  84. return [FIRDocumentReference referenceWithKey:key firestore:self.firestore];
  85. }
  86. }
  87. - (NSString *)path {
  88. return util::WrapNSString(self.query.path.CanonicalString());
  89. }
  90. - (FIRDocumentReference *)documentWithPath:(NSString *)documentPath {
  91. if (!documentPath) {
  92. FSTThrowInvalidArgument(@"Document path cannot be nil.");
  93. }
  94. const ResourcePath subPath = ResourcePath::FromString(util::MakeString(documentPath));
  95. const ResourcePath path = self.query.path.Append(subPath);
  96. return [FIRDocumentReference referenceWithPath:path firestore:self.firestore];
  97. }
  98. - (FIRDocumentReference *)addDocumentWithData:(NSDictionary<NSString *, id> *)data {
  99. return [self addDocumentWithData:data completion:nil];
  100. }
  101. - (FIRDocumentReference *)addDocumentWithData:(NSDictionary<NSString *, id> *)data
  102. completion:
  103. (nullable void (^)(NSError *_Nullable error))completion {
  104. FIRDocumentReference *docRef = [self documentWithAutoID];
  105. [docRef setData:data completion:completion];
  106. return docRef;
  107. }
  108. - (FIRDocumentReference *)documentWithAutoID {
  109. const DocumentKey key{self.query.path.Append(CreateAutoId())};
  110. return [FIRDocumentReference referenceWithKey:key firestore:self.firestore];
  111. }
  112. @end
  113. NS_ASSUME_NONNULL_END