RLMRealmConfiguration+Sync.mm 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. ////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright 2016 Realm Inc.
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "License");
  6. // you may not use this file except in compliance with the License.
  7. // You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. //
  17. ////////////////////////////////////////////////////////////////////////////
  18. #import "RLMRealmConfiguration+Sync.h"
  19. #import "RLMRealmConfiguration_Private.hpp"
  20. #import "RLMSyncConfiguration_Private.hpp"
  21. #import "RLMSyncUser_Private.hpp"
  22. #import "RLMSyncManager_Private.h"
  23. #import "RLMSyncUtil_Private.hpp"
  24. #import "RLMUtil.hpp"
  25. #import "sync/sync_config.hpp"
  26. #import "sync/sync_manager.hpp"
  27. @implementation RLMRealmConfiguration (Sync)
  28. #pragma mark - API
  29. - (void)setSyncConfiguration:(RLMSyncConfiguration *)syncConfiguration {
  30. if (syncConfiguration == nil) {
  31. self.config.sync_config = nullptr;
  32. return;
  33. }
  34. if (self.config.should_compact_on_launch_function) {
  35. @throw RLMException(@"Cannot set `syncConfiguration` when `shouldCompactOnLaunch` is set.");
  36. }
  37. RLMSyncUser *user = syncConfiguration.user;
  38. if (user.state == RLMSyncUserStateError) {
  39. @throw RLMException(@"Cannot set a sync configuration which has an errored-out user.");
  40. }
  41. // Ensure sync manager is initialized, if it hasn't already been.
  42. [RLMSyncManager sharedManager];
  43. NSAssert(user.identity, @"Cannot call this method on a user that doesn't have an identity.");
  44. self.config.in_memory = false;
  45. self.config.sync_config = std::make_shared<realm::SyncConfig>([syncConfiguration rawConfiguration]);
  46. self.config.schema_mode = realm::SchemaMode::Additive;
  47. if (syncConfiguration.customFileURL) {
  48. self.config.path = syncConfiguration.customFileURL.path.UTF8String;
  49. } else {
  50. self.config.path = SyncManager::shared().path_for_realm(*[user _syncUser],
  51. self.config.sync_config->realm_url());
  52. }
  53. if (!self.config.encryption_key.empty()) {
  54. auto& sync_encryption_key = self.config.sync_config->realm_encryption_key;
  55. sync_encryption_key = std::array<char, 64>();
  56. std::copy_n(self.config.encryption_key.begin(), 64, sync_encryption_key->begin());
  57. }
  58. }
  59. - (RLMSyncConfiguration *)syncConfiguration {
  60. if (!self.config.sync_config) {
  61. return nil;
  62. }
  63. realm::SyncConfig& sync_config = *self.config.sync_config;
  64. return [[RLMSyncConfiguration alloc] initWithRawConfig:sync_config];
  65. }
  66. @end