FIRMessagingPendingTopicsList.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * Copyright 2017 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import <Foundation/Foundation.h>
  17. #import "FIRMessagingTopicsCommon.h"
  18. NS_ASSUME_NONNULL_BEGIN
  19. /**
  20. * Represents a single batch of topics, with the same action.
  21. *
  22. * Topic operations which have the same action (subscribe or unsubscribe) can be executed
  23. * simultaneously, as the order of operations do not matter with the same action. The set of
  24. * topics is unique, as it doesn't make sense to apply the same action to the same topic
  25. * repeatedly; the result would be the same as the first time.
  26. */
  27. @interface FIRMessagingTopicBatch : NSObject <NSCoding>
  28. @property(nonatomic, readonly, assign) FIRMessagingTopicAction action;
  29. @property(nonatomic, readonly, copy) NSMutableSet <NSString *> *topics;
  30. - (instancetype)init NS_UNAVAILABLE;
  31. - (instancetype)initWithAction:(FIRMessagingTopicAction)action NS_DESIGNATED_INITIALIZER;
  32. @end
  33. @class FIRMessagingPendingTopicsList;
  34. /**
  35. * This delegate must be supplied to the instance of FIRMessagingPendingTopicsList, via the
  36. * @cdelegate property. It lets the
  37. * pending topics list know whether or not it can begin making requests via
  38. * @c-pendingTopicsListCanRequestTopicUpdates:, and handles the request to actually
  39. * perform the topic operation. The delegate also handles when the pending topics list is updated,
  40. * so that it can be archived or persisted.
  41. *
  42. * @see FIRMessagingPendingTopicsList
  43. */
  44. @protocol FIRMessagingPendingTopicsListDelegate <NSObject>
  45. - (void)pendingTopicsList:(FIRMessagingPendingTopicsList *)list
  46. requestedUpdateForTopic:(NSString *)topic
  47. action:(FIRMessagingTopicAction)action
  48. completion:(FIRMessagingTopicOperationCompletion)completion;
  49. - (void)pendingTopicsListDidUpdate:(FIRMessagingPendingTopicsList *)list;
  50. - (BOOL)pendingTopicsListCanRequestTopicUpdates:(FIRMessagingPendingTopicsList *)list;
  51. @end
  52. /**
  53. * FIRMessagingPendingTopicsList manages a list of topic subscription updates, batched by the same
  54. * action (subscribe or unsubscribe). The list roughly maintains the order of the topic operations,
  55. * batched together whenever the topic action (subscribe or unsubscribe) changes.
  56. *
  57. * Topics operations are batched by action because it is safe to perform the same topic action
  58. * (subscribe or unsubscribe) on many topics simultaneously. After each batch is successfully
  59. * completed, the next batch operations can begin.
  60. *
  61. * When asked to resume its operations, FIRMessagingPendingTopicsList will begin performing updates
  62. * of its current batch of topics. For example, it may begin subscription operations for topics
  63. * [A, B, C] simultaneously.
  64. *
  65. * When the current batch is completed, the next batch of operations will be started. For example
  66. * the list may begin unsubscribe operations for [D, A, E]. Note that because A is in both batches,
  67. * A will be correctly subscribed in the first batch, then unsubscribed as part of the second batch
  68. * of operations. Without batching, it would be ambiguous whether A's subscription operation or the
  69. * unsubscription operation would be completed first.
  70. *
  71. * An app can subscribe and unsubscribe from many topics, and this class helps persist the pending
  72. * topics and perform the operation safely and correctly.
  73. *
  74. * When a topic fails to subscribe or unsubscribe due to a network error, it is considered a
  75. * recoverable error, and so it remains in the current batch until it is succesfully completed.
  76. * Topic updates are completed when they either (a) succeed, (b) are cancelled, or (c) result in an
  77. * unrecoverable error. Any error outside of `NSURLErrorDomain` is considered an unrecoverable
  78. * error.
  79. *
  80. * In addition to maintaining the list of pending topic updates, FIRMessagingPendingTopicsList also
  81. * can track completion handlers for topic operations.
  82. *
  83. * @discussion Completion handlers for topic updates are not maintained if it was restored from a
  84. * keyed archive. They are only called if the topic operation finished within the same app session.
  85. *
  86. * You must supply an object conforming to FIRMessagingPendingTopicsListDelegate in order for the
  87. * topic operations to execute.
  88. *
  89. * @see FIRMessagingPendingTopicsListDelegate
  90. */
  91. @interface FIRMessagingPendingTopicsList : NSObject <NSCoding>
  92. @property(nonatomic, weak) NSObject <FIRMessagingPendingTopicsListDelegate> *delegate;
  93. @property(nonatomic, readonly, strong, nullable) NSDate *archiveDate;
  94. @property(nonatomic, readonly) NSUInteger numberOfBatches;
  95. - (instancetype)init NS_DESIGNATED_INITIALIZER;
  96. - (void)addOperationForTopic:(NSString *)topic
  97. withAction:(FIRMessagingTopicAction)action
  98. completion:(nullable FIRMessagingTopicOperationCompletion)completion;
  99. - (void)resumeOperationsIfNeeded;
  100. @end
  101. NS_ASSUME_NONNULL_END