RLMSyncSession.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 bound to the Realm Object Server and communicating with it.
  25. RLMSyncSessionStateActive,
  26. /// The sync session is not currently communicating with the Realm Object Server.
  27. RLMSyncSessionStateInactive,
  28. /// The sync session encountered a fatal error and is permanently invalid; it should be discarded.
  29. RLMSyncSessionStateInvalid
  30. };
  31. /**
  32. The transfer direction (upload or download) tracked by a given progress notification block.
  33. Progress notification blocks can be registered on sessions if your app wishes to be informed
  34. how many bytes have been uploaded or downloaded, for example to show progress indicator UIs.
  35. */
  36. typedef NS_ENUM(NSUInteger, RLMSyncProgressDirection) {
  37. /// For monitoring upload progress.
  38. RLMSyncProgressDirectionUpload,
  39. /// For monitoring download progress.
  40. RLMSyncProgressDirectionDownload,
  41. };
  42. /**
  43. The desired behavior of a progress notification block.
  44. Progress notification blocks can be registered on sessions if your app wishes to be informed
  45. how many bytes have been uploaded or downloaded, for example to show progress indicator UIs.
  46. */
  47. typedef NS_ENUM(NSUInteger, RLMSyncProgress) {
  48. /**
  49. The block will be called indefinitely, or until it is unregistered by calling
  50. `-[RLMProgressNotificationToken stop]`.
  51. Notifications will always report the latest number of transferred bytes, and the
  52. most up-to-date number of total transferrable bytes.
  53. */
  54. RLMSyncProgressReportIndefinitely,
  55. /**
  56. The block will, upon registration, store the total number of bytes
  57. to be transferred. When invoked, it will always report the most up-to-date number
  58. of transferrable bytes out of that original number of transferrable bytes.
  59. When the number of transferred bytes reaches or exceeds the
  60. number of transferrable bytes, the block will be unregistered.
  61. */
  62. RLMSyncProgressForCurrentlyOutstandingWork,
  63. };
  64. @class RLMSyncUser, RLMSyncConfiguration;
  65. /**
  66. The type of a progress notification block intended for reporting a session's network
  67. activity to the user.
  68. `transferredBytes` refers to the number of bytes that have been uploaded or downloaded.
  69. `transferrableBytes` refers to the total number of bytes transferred, and pending transfer.
  70. */
  71. typedef void(^RLMProgressNotificationBlock)(NSUInteger transferredBytes, NSUInteger transferrableBytes);
  72. NS_ASSUME_NONNULL_BEGIN
  73. /**
  74. A token object corresponding to a progress notification block on a session object.
  75. To stop notifications manually, call `-stop` on it. Notifications should be stopped before
  76. the token goes out of scope or is destroyed.
  77. */
  78. @interface RLMProgressNotificationToken : RLMNotificationToken
  79. @end
  80. /**
  81. An object encapsulating a Realm Object Server "session". Sessions represent the
  82. communication between the client (and a local Realm file on disk), and the server
  83. (and a remote Realm at a given URL stored on a Realm Object Server).
  84. Sessions are always created by the SDK and vended out through various APIs. The
  85. lifespans of sessions associated with Realms are managed automatically. Session
  86. objects can be accessed from any thread.
  87. */
  88. @interface RLMSyncSession : NSObject
  89. /// The session's current state.
  90. @property (nonatomic, readonly) RLMSyncSessionState state;
  91. /// The Realm Object Server URL of the remote Realm this session corresponds to.
  92. @property (nullable, nonatomic, readonly) NSURL *realmURL;
  93. /// The user that owns this session.
  94. - (nullable RLMSyncUser *)parentUser;
  95. /**
  96. If the session is valid, return a sync configuration that can be used to open the Realm
  97. associated with this session.
  98. */
  99. - (nullable RLMSyncConfiguration *)configuration;
  100. /**
  101. Register a progress notification block.
  102. Multiple blocks can be registered with the same session at once. Each block
  103. will be invoked on a side queue devoted to progress notifications.
  104. If the session has already received progress information from the
  105. synchronization subsystem, the block will be called immediately. Otherwise, it
  106. will be called as soon as progress information becomes available.
  107. The token returned by this method must be retained as long as progress
  108. notifications are desired, and the `-stop` method should be called on it
  109. when notifications are no longer needed and before the token is destroyed.
  110. If no token is returned, the notification block will never be called again.
  111. There are a number of reasons this might be true. If the session has previously
  112. experienced a fatal error it will not accept progress notification blocks. If
  113. the block was configured in the `RLMSyncProgressForCurrentlyOutstandingWork`
  114. mode but there is no additional progress to report (for example, the number
  115. of transferrable bytes and transferred bytes are equal), the block will not be
  116. called again.
  117. @param direction The transfer direction (upload or download) to track in this progress notification block.
  118. @param mode The desired behavior of this progress notification block.
  119. @param block The block to invoke when notifications are available.
  120. @return A token which must be held for as long as you want notifications to be delivered.
  121. @see `RLMSyncProgressDirection`, `RLMSyncProgress`, `RLMProgressNotificationBlock`, `RLMProgressNotificationToken`
  122. */
  123. - (nullable RLMProgressNotificationToken *)addProgressNotificationForDirection:(RLMSyncProgressDirection)direction
  124. mode:(RLMSyncProgress)mode
  125. block:(RLMProgressNotificationBlock)block
  126. NS_REFINED_FOR_SWIFT;
  127. @end
  128. NS_ASSUME_NONNULL_END