FIRFirestoreSettings.mm 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. #include "Firestore/core/src/firebase/firestore/util/warnings.h"
  17. #import "FIRFirestoreSettings.h"
  18. #import "Firestore/Source/Util/FSTUsageValidation.h"
  19. NS_ASSUME_NONNULL_BEGIN
  20. static NSString *const kDefaultHost = @"firestore.googleapis.com";
  21. static const BOOL kDefaultSSLEnabled = YES;
  22. static const BOOL kDefaultPersistenceEnabled = YES;
  23. static const int64_t kDefaultCacheSizeBytes = 100 * 1024 * 1024;
  24. static const int64_t kMinimumCacheSizeBytes = 1 * 1024 * 1024;
  25. static const BOOL kDefaultTimestampsInSnapshotsEnabled = YES;
  26. @implementation FIRFirestoreSettings
  27. - (instancetype)init {
  28. if (self = [super init]) {
  29. _host = kDefaultHost;
  30. _sslEnabled = kDefaultSSLEnabled;
  31. _dispatchQueue = dispatch_get_main_queue();
  32. _persistenceEnabled = kDefaultPersistenceEnabled;
  33. _timestampsInSnapshotsEnabled = kDefaultTimestampsInSnapshotsEnabled;
  34. _cacheSizeBytes = kDefaultCacheSizeBytes;
  35. }
  36. return self;
  37. }
  38. - (BOOL)isEqual:(id)other {
  39. if (self == other) {
  40. return YES;
  41. } else if (![other isKindOfClass:[FIRFirestoreSettings class]]) {
  42. return NO;
  43. }
  44. FIRFirestoreSettings *otherSettings = (FIRFirestoreSettings *)other;
  45. SUPPRESS_DEPRECATED_DECLARATIONS_BEGIN()
  46. return [self.host isEqual:otherSettings.host] &&
  47. self.isSSLEnabled == otherSettings.isSSLEnabled &&
  48. self.dispatchQueue == otherSettings.dispatchQueue &&
  49. self.isPersistenceEnabled == otherSettings.isPersistenceEnabled &&
  50. self.timestampsInSnapshotsEnabled == otherSettings.timestampsInSnapshotsEnabled &&
  51. self.cacheSizeBytes == otherSettings.cacheSizeBytes;
  52. SUPPRESS_END()
  53. }
  54. - (NSUInteger)hash {
  55. NSUInteger result = [self.host hash];
  56. result = 31 * result + (self.isSSLEnabled ? 1231 : 1237);
  57. // Ignore the dispatchQueue to avoid having to deal with sizeof(dispatch_queue_t).
  58. result = 31 * result + (self.isPersistenceEnabled ? 1231 : 1237);
  59. SUPPRESS_DEPRECATED_DECLARATIONS_BEGIN()
  60. result = 31 * result + (self.timestampsInSnapshotsEnabled ? 1231 : 1237);
  61. SUPPRESS_END()
  62. result = 31 * result + (NSUInteger)self.cacheSizeBytes;
  63. return result;
  64. }
  65. - (id)copyWithZone:(nullable NSZone *)zone {
  66. FIRFirestoreSettings *copy = [[FIRFirestoreSettings alloc] init];
  67. copy.host = _host;
  68. copy.sslEnabled = _sslEnabled;
  69. copy.dispatchQueue = _dispatchQueue;
  70. copy.persistenceEnabled = _persistenceEnabled;
  71. SUPPRESS_DEPRECATED_DECLARATIONS_BEGIN()
  72. copy.timestampsInSnapshotsEnabled = _timestampsInSnapshotsEnabled;
  73. SUPPRESS_END()
  74. copy.cacheSizeBytes = _cacheSizeBytes;
  75. return copy;
  76. }
  77. - (void)setHost:(NSString *)host {
  78. if (!host) {
  79. FSTThrowInvalidArgument(
  80. @"host setting may not be nil. You should generally just use the default value "
  81. "(which is %@)",
  82. kDefaultHost);
  83. }
  84. _host = [host mutableCopy];
  85. }
  86. - (void)setDispatchQueue:(dispatch_queue_t)dispatchQueue {
  87. if (!dispatchQueue) {
  88. FSTThrowInvalidArgument(
  89. @"dispatch queue setting may not be nil. Create a new dispatch queue with "
  90. "dispatch_queue_create(\"com.example.MyQueue\", NULL) or just use the default "
  91. "(which is the main queue, returned from dispatch_get_main_queue())");
  92. }
  93. _dispatchQueue = dispatchQueue;
  94. }
  95. - (void)setCacheSizeBytes:(int64_t)cacheSizeBytes {
  96. if (cacheSizeBytes != kFIRFirestoreCacheSizeUnlimited &&
  97. cacheSizeBytes < kMinimumCacheSizeBytes) {
  98. FSTThrowInvalidArgument(@"Cache size must be set to at least %i bytes", kMinimumCacheSizeBytes);
  99. }
  100. _cacheSizeBytes = cacheSizeBytes;
  101. }
  102. @end
  103. NS_ASSUME_NONNULL_END