RLMRealm+Sync.mm 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. ////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright 2017 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 "RLMRealm+Sync.h"
  19. #import "RLMObjectBase.h"
  20. #import "RLMQueryUtil.hpp"
  21. #import "RLMObjectSchema.h"
  22. #import "RLMRealm_Private.hpp"
  23. #import "RLMResults_Private.hpp"
  24. #import "RLMSchema.h"
  25. #import "RLMSyncSession.h"
  26. #import "results.hpp"
  27. #import "shared_realm.hpp"
  28. #import "sync/partial_sync.hpp"
  29. #import "sync/subscription_state.hpp"
  30. using namespace realm;
  31. @implementation RLMRealm (Sync)
  32. - (void)subscribeToObjects:(Class)type where:(NSString *)query callback:(RLMPartialSyncFetchCallback)callback {
  33. [self verifyThread];
  34. RLMClassInfo& info = _info[[type className]];
  35. Query q = RLMPredicateToQuery([NSPredicate predicateWithFormat:query],
  36. info.rlmObjectSchema, self.schema, self.group);
  37. struct Holder {
  38. partial_sync::Subscription subscription;
  39. partial_sync::SubscriptionNotificationToken token;
  40. Holder(partial_sync::Subscription&& s) : subscription(std::move(s)) { }
  41. };
  42. auto state = std::make_shared<Holder>(partial_sync::subscribe(Results(_realm, std::move(q)), {}));
  43. state->token = state->subscription.add_notification_callback([=]() mutable {
  44. if (!callback) {
  45. return;
  46. }
  47. switch (state->subscription.state()) {
  48. case partial_sync::SubscriptionState::Invalidated:
  49. case partial_sync::SubscriptionState::Pending:
  50. case partial_sync::SubscriptionState::Creating:
  51. return;
  52. case partial_sync::SubscriptionState::Error:
  53. try {
  54. rethrow_exception(state->subscription.error());
  55. }
  56. catch (...) {
  57. NSError *error = nil;
  58. RLMRealmTranslateException(&error);
  59. callback(nil, error);
  60. }
  61. break;
  62. case partial_sync::SubscriptionState::Complete:
  63. callback([RLMResults emptyDetachedResults], nil);
  64. break;
  65. }
  66. callback = nil;
  67. state->token = {};
  68. });
  69. }
  70. - (RLMSyncSession *)syncSession {
  71. return [RLMSyncSession sessionForRealm:self];
  72. }
  73. @end