FIRDatabaseConfig.m 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 "FIRDatabaseConfig.h"
  17. #import "FAuthTokenProvider.h"
  18. #import "FIRDatabaseConfig_Private.h"
  19. #import "FIRNoopAuthTokenProvider.h"
  20. @interface FIRDatabaseConfig (Private)
  21. @property(nonatomic, strong, readwrite) NSString *sessionIdentifier;
  22. @end
  23. @implementation FIRDatabaseConfig
  24. - (id)init {
  25. [NSException raise:NSInvalidArgumentException
  26. format:@"Can't create config objects!"];
  27. return nil;
  28. }
  29. - (id)initWithSessionIdentifier:(NSString *)identifier
  30. authTokenProvider:(id<FAuthTokenProvider>)authTokenProvider {
  31. self = [super init];
  32. if (self != nil) {
  33. self->_sessionIdentifier = identifier;
  34. self->_callbackQueue = dispatch_get_main_queue();
  35. self->_persistenceCacheSizeBytes =
  36. 10 * 1024 * 1024; // Default cache size is 10MB
  37. self->_authTokenProvider = authTokenProvider;
  38. }
  39. return self;
  40. }
  41. - (void)assertUnfrozen {
  42. if (self.isFrozen) {
  43. [NSException raise:NSGenericException
  44. format:@"Can't modify config objects after they are in use "
  45. @"for FIRDatabaseReferences."];
  46. }
  47. }
  48. - (void)setAuthTokenProvider:(id<FAuthTokenProvider>)authTokenProvider {
  49. [self assertUnfrozen];
  50. self->_authTokenProvider = authTokenProvider;
  51. }
  52. - (void)setPersistenceEnabled:(BOOL)persistenceEnabled {
  53. [self assertUnfrozen];
  54. self->_persistenceEnabled = persistenceEnabled;
  55. }
  56. - (void)setPersistenceCacheSizeBytes:(NSUInteger)persistenceCacheSizeBytes {
  57. [self assertUnfrozen];
  58. // Can't be less than 1MB
  59. if (persistenceCacheSizeBytes < 1024 * 1024) {
  60. [NSException raise:NSInvalidArgumentException
  61. format:@"The minimum cache size must be at least 1MB"];
  62. }
  63. if (persistenceCacheSizeBytes > 100 * 1024 * 1024) {
  64. [NSException raise:NSInvalidArgumentException
  65. format:@"Firebase Database currently doesn't support a "
  66. @"cache size larger than 100MB"];
  67. }
  68. self->_persistenceCacheSizeBytes = persistenceCacheSizeBytes;
  69. }
  70. - (void)setCallbackQueue:(dispatch_queue_t)callbackQueue {
  71. [self assertUnfrozen];
  72. self->_callbackQueue = callbackQueue;
  73. }
  74. - (void)freeze {
  75. self->_isFrozen = YES;
  76. }
  77. @end