RLMSyncSession.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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 <Foundation/Foundation.h>
  19. #import "RLMRealm.h"
  20. /**
  21. The current state of the session represented by a session object.
  22. */
  23. typedef NS_ENUM(NSUInteger, RLMSyncSessionState) {
  24. /// The sync session is actively communicating or attempting to communicate
  25. /// with the Realm Object Server. A session is considered Active even if
  26. /// it is not currently connected. Check the connection state instead if you
  27. /// wish to know if the connection is currently online.
  28. RLMSyncSessionStateActive,
  29. /// The sync session is not attempting to communicate with the Realm Object
  30. /// Server, due to the user logging out or synchronization being paused.
  31. RLMSyncSessionStateInactive,
  32. /// The sync session encountered a fatal error and is permanently invalid; it should be discarded.
  33. RLMSyncSessionStateInvalid
  34. };
  35. /**
  36. The current state of a sync session's connection. Sessions which are not in
  37. the Active state will always be Disconnected.
  38. */
  39. typedef NS_ENUM(NSUInteger, RLMSyncConnectionState) {
  40. /// The sync session is not connected to the server, and is not attempting
  41. /// to connect, either because the session is inactive or because it is
  42. /// waiting to retry after a failed connection.
  43. RLMSyncConnectionStateDisconnected,
  44. /// The sync session is attempting to connect to the Realm Object Server.
  45. RLMSyncConnectionStateConnecting,
  46. /// The sync session is currently connected to the Realm Object Server.
  47. RLMSyncConnectionStateConnected,
  48. };
  49. /**
  50. The transfer direction (upload or download) tracked by a given progress notification block.
  51. Progress notification blocks can be registered on sessions if your app wishes to be informed
  52. how many bytes have been uploaded or downloaded, for example to show progress indicator UIs.
  53. */
  54. typedef RLM_CLOSED_ENUM(NSUInteger, RLMSyncProgressDirection) {
  55. /// For monitoring upload progress.
  56. RLMSyncProgressDirectionUpload,
  57. /// For monitoring download progress.
  58. RLMSyncProgressDirectionDownload,
  59. };
  60. /**
  61. The desired behavior of a progress notification block.
  62. Progress notification blocks can be registered on sessions if your app wishes to be informed
  63. how many bytes have been uploaded or downloaded, for example to show progress indicator UIs.
  64. */
  65. typedef NS_ENUM(NSUInteger, RLMSyncProgressMode) {
  66. /**
  67. The block will be called indefinitely, or until it is unregistered by calling
  68. `-[RLMProgressNotificationToken invalidate]`.
  69. Notifications will always report the latest number of transferred bytes, and the
  70. most up-to-date number of total transferrable bytes.
  71. */
  72. RLMSyncProgressModeReportIndefinitely,
  73. /**
  74. The block will, upon registration, store the total number of bytes
  75. to be transferred. When invoked, it will always report the most up-to-date number
  76. of transferrable bytes out of that original number of transferrable bytes.
  77. When the number of transferred bytes reaches or exceeds the
  78. number of transferrable bytes, the block will be unregistered.
  79. */
  80. RLMSyncProgressModeForCurrentlyOutstandingWork,
  81. };
  82. @class RLMSyncUser, RLMSyncConfiguration, RLMSyncErrorActionToken;
  83. /**
  84. The type of a progress notification block intended for reporting a session's network
  85. activity to the user.
  86. `transferredBytes` refers to the number of bytes that have been uploaded or downloaded.
  87. `transferrableBytes` refers to the total number of bytes transferred, and pending transfer.
  88. */
  89. typedef void(^RLMProgressNotificationBlock)(NSUInteger transferredBytes, NSUInteger transferrableBytes);
  90. NS_ASSUME_NONNULL_BEGIN
  91. /**
  92. A token object corresponding to a progress notification block on a session object.
  93. To stop notifications manually, call `-invalidate` on it. Notifications should be stopped before
  94. the token goes out of scope or is destroyed.
  95. */
  96. @interface RLMProgressNotificationToken : RLMNotificationToken
  97. @end
  98. /**
  99. An object encapsulating a Realm Object Server "session". Sessions represent the
  100. communication between the client (and a local Realm file on disk), and the server
  101. (and a remote Realm at a given URL stored on a Realm Object Server).
  102. Sessions are always created by the SDK and vended out through various APIs. The
  103. lifespans of sessions associated with Realms are managed automatically. Session
  104. objects can be accessed from any thread.
  105. */
  106. @interface RLMSyncSession : NSObject
  107. /// The session's current state.
  108. ///
  109. /// This property is not KVO-compliant.
  110. @property (nonatomic, readonly) RLMSyncSessionState state;
  111. /// The session's current connection state.
  112. ///
  113. /// This property is KVO-compliant and can be observed to be notified of changes.
  114. /// Be warned that KVO observers for this property may be called on a background
  115. /// thread.
  116. @property (atomic, readonly) RLMSyncConnectionState connectionState;
  117. /// The Realm Object Server URL of the remote Realm this session corresponds to.
  118. @property (nullable, nonatomic, readonly) NSURL *realmURL;
  119. /// The user that owns this session.
  120. - (nullable RLMSyncUser *)parentUser;
  121. /**
  122. If the session is valid, return a sync configuration that can be used to open the Realm
  123. associated with this session.
  124. */
  125. - (nullable RLMSyncConfiguration *)configuration;
  126. /**
  127. Temporarily suspend syncronization and disconnect from the server.
  128. The session will not attempt to connect to Realm Object Server until `resume`
  129. is called or the Realm file is closed and re-opened.
  130. */
  131. - (void)suspend;
  132. /**
  133. Resume syncronization and reconnect to Realm Object Server after suspending.
  134. This is a no-op if the session was already active or if the session is invalid.
  135. Newly created sessions begin in the Active state and do not need to be resumed.
  136. */
  137. - (void)resume;
  138. /**
  139. Register a progress notification block.
  140. Multiple blocks can be registered with the same session at once. Each block
  141. will be invoked on a side queue devoted to progress notifications.
  142. If the session has already received progress information from the
  143. synchronization subsystem, the block will be called immediately. Otherwise, it
  144. will be called as soon as progress information becomes available.
  145. The token returned by this method must be retained as long as progress
  146. notifications are desired, and the `-invalidate` method should be called on it
  147. when notifications are no longer needed and before the token is destroyed.
  148. If no token is returned, the notification block will never be called again.
  149. There are a number of reasons this might be true. If the session has previously
  150. experienced a fatal error it will not accept progress notification blocks. If
  151. the block was configured in the `RLMSyncProgressForCurrentlyOutstandingWork`
  152. mode but there is no additional progress to report (for example, the number
  153. of transferrable bytes and transferred bytes are equal), the block will not be
  154. called again.
  155. @param direction The transfer direction (upload or download) to track in this progress notification block.
  156. @param mode The desired behavior of this progress notification block.
  157. @param block The block to invoke when notifications are available.
  158. @return A token which must be held for as long as you want notifications to be delivered.
  159. @see `RLMSyncProgressDirection`, `RLMSyncProgress`, `RLMProgressNotificationBlock`, `RLMProgressNotificationToken`
  160. */
  161. - (nullable RLMProgressNotificationToken *)addProgressNotificationForDirection:(RLMSyncProgressDirection)direction
  162. mode:(RLMSyncProgressMode)mode
  163. block:(RLMProgressNotificationBlock)block
  164. NS_REFINED_FOR_SWIFT;
  165. /**
  166. Given an error action token, immediately handle the corresponding action.
  167. @see `RLMSyncErrorClientResetError`, `RLMSyncErrorPermissionDeniedError`
  168. */
  169. + (void)immediatelyHandleError:(RLMSyncErrorActionToken *)token;
  170. /**
  171. Get the sync session for the given Realm if it is a synchronized Realm, or `nil`
  172. if it is not.
  173. */
  174. + (nullable RLMSyncSession *)sessionForRealm:(RLMRealm *)realm;
  175. @end
  176. // MARK: - Error action token
  177. #pragma mark - Error action token
  178. /**
  179. An opaque token returned as part of certain errors. It can be
  180. passed into certain APIs to perform certain actions.
  181. @see `RLMSyncErrorClientResetError`, `RLMSyncErrorPermissionDeniedError`
  182. */
  183. @interface RLMSyncErrorActionToken : NSObject
  184. /// :nodoc:
  185. - (instancetype)init __attribute__((unavailable("This type cannot be created directly")));
  186. /// :nodoc:
  187. + (instancetype)new __attribute__((unavailable("This type cannot be created directly")));
  188. @end
  189. /**
  190. A task object which can be used to observe or cancel an async open.
  191. When a synchronized Realm is opened asynchronously, the latest state of the
  192. Realm is downloaded from the server before the completion callback is invoked.
  193. This task object can be used to observe the state of the download or to cancel
  194. it. This should be used instead of trying to observe the download via the sync
  195. session as the sync session itself is created asynchronously, and may not exist
  196. yet when -[RLMRealm asyncOpenWithConfiguration:completion:] returns.
  197. */
  198. @interface RLMAsyncOpenTask : NSObject
  199. /**
  200. Register a progress notification block.
  201. Each registered progress notification block is called whenever the sync
  202. subsystem has new progress data to report until the task is either cancelled
  203. or the completion callback is called. Progress notifications are delivered on
  204. the main queue.
  205. */
  206. - (void)addProgressNotificationBlock:(RLMProgressNotificationBlock)block;
  207. /**
  208. Register a progress notification block which is called on the given queue.
  209. Each registered progress notification block is called whenever the sync
  210. subsystem has new progress data to report until the task is either cancelled
  211. or the completion callback is called. Progress notifications are delivered on
  212. the supplied queue.
  213. */
  214. - (void)addProgressNotificationOnQueue:(dispatch_queue_t)queue
  215. block:(RLMProgressNotificationBlock)block;
  216. /**
  217. Cancel the asynchronous open.
  218. Any download in progress will be cancelled, and the completion block for this
  219. async open will never be called. If multiple async opens on the same Realm are
  220. happening concurrently, all other opens will fail with the error "operation cancelled".
  221. */
  222. - (void)cancel;
  223. @end
  224. NS_ASSUME_NONNULL_END