FIRFirestoreSettings.mm 4.2 KB

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