RLMSyncSubscription.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. Subscribing to a query asks the server to synchronize all objects to the
  94. client which match the query, along with all objects which are reachable
  95. from those objects via links. This happens asynchronously, and the local
  96. client Realm may not immediately have all objects which match the query.
  97. Observe the `state` property of the returned subscription object to be
  98. notified of when the subscription has been processed by the server and
  99. all objects matching the query are available.
  100. The subscription will not be explicitly named.
  101. @return The subscription
  102. @see RLMSyncSubscription
  103. */
  104. - (RLMSyncSubscription *)subscribe;
  105. /**
  106. Subscribe to the query represented by this `RLMResults`.
  107. Subscribing to a query asks the server to synchronize all objects to the
  108. client which match the query, along with all objects which are reachable
  109. from those objects via links. This happens asynchronously, and the local
  110. client Realm may not immediately have all objects which match the query.
  111. Observe the `state` property of the returned subscription object to be
  112. notified of when the subscription has been processed by the server and
  113. all objects matching the query are available.
  114. Creating a new subscription with the same name and query as an existing
  115. subscription will not create a new subscription, but instead will return
  116. an object referring to the existing sync subscription. This means that
  117. performing the same subscription twice followed by removing it once will
  118. result in no subscription existing.
  119. @param subscriptionName The name of the subscription
  120. @return The subscription
  121. @see RLMSyncSubscription
  122. */
  123. - (RLMSyncSubscription *)subscribeWithName:(nullable NSString *)subscriptionName;
  124. /**
  125. Subscribe to a subset of the query represented by this `RLMResults`.
  126. Subscribing to a query asks the server to synchronize all objects to the
  127. client which match the query, along with all objects which are reachable
  128. from those objects via links. This happens asynchronously, and the local
  129. client Realm may not immediately have all objects which match the query.
  130. Observe the `state` property of the returned subscription object to be
  131. notified of when the subscription has been processed by the server and
  132. all objects matching the query are available.
  133. Creating a new subscription with the same name and query as an existing
  134. subscription will not create a new subscription, but instead will return
  135. an object referring to the existing sync subscription. This means that
  136. performing the same subscription twice followed by removing it once will
  137. result in no subscription existing.
  138. The number of top-level matches may optionally be limited. This limit
  139. respects the sort and distinct order of the query being subscribed to,
  140. if any. Please note that the limit does not count or apply to objects
  141. which are added indirectly due to being linked to by the objects in the
  142. subscription. If the limit is larger than the number of objects which
  143. match the query, all objects will be included. Limiting a subscription
  144. requires ROS 3.10.1 or newer, and will fail with an invalid predicate
  145. error with older versions.
  146. @param subscriptionName The name of the subscription
  147. @param limit The maximum number of objects to include in the subscription.
  148. @return The subscription
  149. @see RLMSyncSubscription
  150. */
  151. - (RLMSyncSubscription *)subscribeWithName:(nullable NSString *)subscriptionName limit:(NSUInteger)limit;
  152. @end
  153. NS_ASSUME_NONNULL_END