FIRCollectionReference.mm 4.1 KB

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