2 * Copyright 2017 Google
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #import <Foundation/Foundation.h>
19 #import "FIRMessaging.h"
20 #import "FIRMessagingTopicsCommon.h"
22 NS_ASSUME_NONNULL_BEGIN
25 * Represents a single batch of topics, with the same action.
27 * Topic operations which have the same action (subscribe or unsubscribe) can be executed
28 * simultaneously, as the order of operations do not matter with the same action. The set of
29 * topics is unique, as it doesn't make sense to apply the same action to the same topic
30 * repeatedly; the result would be the same as the first time.
32 @interface FIRMessagingTopicBatch : NSObject <NSCoding>
34 @property(nonatomic, readonly, assign) FIRMessagingTopicAction action;
35 @property(nonatomic, readonly, copy) NSMutableSet <NSString *> *topics;
37 - (instancetype)init NS_UNAVAILABLE;
38 - (instancetype)initWithAction:(FIRMessagingTopicAction)action NS_DESIGNATED_INITIALIZER;
42 @class FIRMessagingPendingTopicsList;
44 * This delegate must be supplied to the instance of FIRMessagingPendingTopicsList, via the
45 * @cdelegate property. It lets the
46 * pending topics list know whether or not it can begin making requests via
47 * @c-pendingTopicsListCanRequestTopicUpdates:, and handles the request to actually
48 * perform the topic operation. The delegate also handles when the pending topics list is updated,
49 * so that it can be archived or persisted.
51 * @see FIRMessagingPendingTopicsList
53 @protocol FIRMessagingPendingTopicsListDelegate <NSObject>
55 - (void)pendingTopicsList:(FIRMessagingPendingTopicsList *)list
56 requestedUpdateForTopic:(NSString *)topic
57 action:(FIRMessagingTopicAction)action
58 completion:(FIRMessagingTopicOperationCompletion)completion;
59 - (void)pendingTopicsListDidUpdate:(FIRMessagingPendingTopicsList *)list;
60 - (BOOL)pendingTopicsListCanRequestTopicUpdates:(FIRMessagingPendingTopicsList *)list;
65 * FIRMessagingPendingTopicsList manages a list of topic subscription updates, batched by the same
66 * action (subscribe or unsubscribe). The list roughly maintains the order of the topic operations,
67 * batched together whenever the topic action (subscribe or unsubscribe) changes.
69 * Topics operations are batched by action because it is safe to perform the same topic action
70 * (subscribe or unsubscribe) on many topics simultaneously. After each batch is successfully
71 * completed, the next batch operations can begin.
73 * When asked to resume its operations, FIRMessagingPendingTopicsList will begin performing updates
74 * of its current batch of topics. For example, it may begin subscription operations for topics
75 * [A, B, C] simultaneously.
77 * When the current batch is completed, the next batch of operations will be started. For example
78 * the list may begin unsubscribe operations for [D, A, E]. Note that because A is in both batches,
79 * A will be correctly subscribed in the first batch, then unsubscribed as part of the second batch
80 * of operations. Without batching, it would be ambiguous whether A's subscription operation or the
81 * unsubscription operation would be completed first.
83 * An app can subscribe and unsubscribe from many topics, and this class helps persist the pending
84 * topics and perform the operation safely and correctly.
86 * When a topic fails to subscribe or unsubscribe due to a network error, it is considered a
87 * recoverable error, and so it remains in the current batch until it is succesfully completed.
88 * Topic updates are completed when they either (a) succeed, (b) are cancelled, or (c) result in an
89 * unrecoverable error. Any error outside of `NSURLErrorDomain` is considered an unrecoverable
92 * In addition to maintaining the list of pending topic updates, FIRMessagingPendingTopicsList also
93 * can track completion handlers for topic operations.
95 * @discussion Completion handlers for topic updates are not maintained if it was restored from a
96 * keyed archive. They are only called if the topic operation finished within the same app session.
98 * You must supply an object conforming to FIRMessagingPendingTopicsListDelegate in order for the
99 * topic operations to execute.
101 * @see FIRMessagingPendingTopicsListDelegate
103 @interface FIRMessagingPendingTopicsList : NSObject <NSCoding>
105 @property(nonatomic, weak) NSObject <FIRMessagingPendingTopicsListDelegate> *delegate;
107 @property(nonatomic, readonly, strong, nullable) NSDate *archiveDate;
108 @property(nonatomic, readonly) NSUInteger numberOfBatches;
111 - (instancetype)init NS_DESIGNATED_INITIALIZER;
112 - (void)addOperationForTopic:(NSString *)topic
113 withAction:(FIRMessagingTopicAction)action
114 completion:(nullable FIRMessagingTopicOperationCompletion)completion;
115 - (void)resumeOperationsIfNeeded;
119 NS_ASSUME_NONNULL_END