FIRCollectionReference.mm 4.7 KB

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