FIRCollectionReference.m 3.8 KB

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