RLMSyncSubscription.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. ////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright 2018 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 <Realm/RLMResults.h>
  19. NS_ASSUME_NONNULL_BEGIN
  20. /**
  21. `RLMSyncSubscriptionState` is an enumeration representing the possible state of a sync subscription.
  22. */
  23. typedef NS_ENUM(NSInteger, RLMSyncSubscriptionState) {
  24. /**
  25. An error occurred while creating the subscription or while the server was processing it.
  26. */
  27. RLMSyncSubscriptionStateError = -1,
  28. /**
  29. The subscription is being created, but has not yet been written to the synced Realm.
  30. */
  31. RLMSyncSubscriptionStateCreating = 2,
  32. /**
  33. The subscription has been created, and is waiting to be processed by the server.
  34. */
  35. RLMSyncSubscriptionStatePending = 0,
  36. /**
  37. The subscription has been processed by the server, and objects matching the subscription
  38. are now being synchronized to this client.
  39. */
  40. RLMSyncSubscriptionStateComplete = 1,
  41. /**
  42. This subscription has been removed.
  43. */
  44. RLMSyncSubscriptionStateInvalidated = 3,
  45. };
  46. /**
  47. `RLMSyncSubscription` represents a subscription to a set of objects in a synced Realm.
  48. When partial sync is enabled for a synced Realm, the only objects that the server synchronizes to the
  49. client are those that match a sync subscription registered by that client. A subscription consists of
  50. of a query (represented by an `RLMResults`) and an optional name.
  51. The state of the subscription can be observed using [Key-Value Observing](https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/KeyValueObserving/KeyValueObserving.html) on the `state` property.
  52. Subscriptions are created using `-[RLMResults subscribe]` or `-[RLMResults subscribeWithName:]`.
  53. */
  54. @interface RLMSyncSubscription : NSObject
  55. /**
  56. The unique name for this subscription.
  57. This will be `nil` if a name was not provided when the subscription was created.
  58. */
  59. @property (nonatomic, readonly, nullable) NSString *name;
  60. /**
  61. The state of the subscription. See `RLMSyncSubscriptionState`.
  62. */
  63. @property (nonatomic, readonly) RLMSyncSubscriptionState state;
  64. /**
  65. The error associated with this subscription, if any.
  66. Will be non-nil only when `state` is `RLMSyncSubscriptionStateError`.
  67. */
  68. @property (nonatomic, readonly, nullable) NSError *error;
  69. /**
  70. Remove this subscription.
  71. Removing a subscription will delete all objects from the local Realm that were matched
  72. only by that subscription and not any remaining subscriptions. The deletion is performed
  73. by the server, and so has no immediate impact on the contents of the local Realm. If the
  74. device is currently offline, the removal will not be processed until the device returns online.
  75. */
  76. - (void)unsubscribe;
  77. #pragma mark - Unavailable Methods
  78. /**
  79. `-[RLMSyncSubscription init]` is not available because `RLMSyncSubscription` cannot be created directly.
  80. */
  81. - (instancetype)init __attribute__((unavailable("RLMSyncSubscription cannot be created directly")));
  82. /**
  83. `+[RLMSyncSubscription new]` is not available because `RLMSyncSubscription` cannot be created directly.
  84. */
  85. + (instancetype)new __attribute__((unavailable("RLMSyncSubscription cannot be created directly")));
  86. @end
  87. /**
  88. Support for subscribing to the results of object queries in a synced Realm.
  89. */
  90. @interface RLMResults (SyncSubscription)
  91. /**
  92. Subscribe to the query represented by this `RLMResults`.
  93. The subscription will not be explicitly named.
  94. @return The subscription
  95. @see RLMSyncSubscription
  96. */
  97. - (RLMSyncSubscription *)subscribe;
  98. /**
  99. Subscribe to the query represented by this `RLMResults`.
  100. @param subscriptionName The name of the subscription
  101. @return The subscription
  102. @see RLMSyncSubscription
  103. */
  104. - (RLMSyncSubscription *)subscribeWithName:(NSString *)subscriptionName;
  105. @end
  106. NS_ASSUME_NONNULL_END