commit | author | age
|
a0a843
|
1 |
// |
H |
2 |
// TFSScribe.h |
|
3 |
// TFSScribe |
|
4 |
// |
|
5 |
// Created by Tanner Oakes on 10/10/14. |
|
6 |
// Copyright (c) 2014 Twitter. All rights reserved. |
|
7 |
// |
|
8 |
|
|
9 |
#import <Foundation/Foundation.h> |
|
10 |
|
|
11 |
#ifndef NS_DESIGNATED_INITIALIZER |
|
12 |
#define NS_DESIGNATED_INITIALIZER __attribute((objc_designated_initializer)) |
|
13 |
#endif |
|
14 |
|
|
15 |
FOUNDATION_EXTERN NSString *const TFSScribeDebugPreferencesKey; |
|
16 |
|
|
17 |
FOUNDATION_EXTERN NSString *const TFSScribeEventNotification; // Triggered when the scribe API call returns |
|
18 |
FOUNDATION_EXTERN NSString *const TFSScribeFlushNotification; // Triggered after scribe flush |
|
19 |
FOUNDATION_EXTERN NSString *const TFSScribeFlushTokenInfoKey; // Key in userInfo dictionary corresponding to the token used in the flush request. |
|
20 |
|
|
21 |
FOUNDATION_EXTERN const NSInteger TFSScribeServiceUpdateValue; |
|
22 |
|
|
23 |
/** |
|
24 |
* Result of handling outgoing scribe events. |
|
25 |
*/ |
|
26 |
typedef NS_ENUM(NSUInteger, TFSScribeServiceRequestDisposition) { |
|
27 |
/** |
|
28 |
* Indicates that the outgoing events were handled successfully. |
|
29 |
*/ |
|
30 |
TFSScribeServiceRequestDispositionSuccess, |
|
31 |
/** |
|
32 |
* Indicates that handling failed due to a client side problem, such as a network |
|
33 |
* timeout or the request being cancelled. |
|
34 |
*/ |
|
35 |
TFSScribeServiceRequestDispositionClientError, |
|
36 |
/** |
|
37 |
* Indicates that handling failed due to the server rejecting the sent data. |
|
38 |
*/ |
|
39 |
TFSScribeServiceRequestDispositionServerError, |
|
40 |
}; |
|
41 |
|
|
42 |
/** |
|
43 |
* Object representing a scribe event. These methods must be thread safe. |
|
44 |
*/ |
|
45 |
@protocol TFSScribeEventParameters <NSObject> |
|
46 |
|
|
47 |
/** |
|
48 |
* Binary representation of the scribe event. This is what will be kept in |
|
49 |
* the store and returned when flush is called. |
|
50 |
*/ |
|
51 |
- (NSData *)data; |
|
52 |
/** |
|
53 |
* Dictionary representation of the event used for logging purposes. |
|
54 |
*/ |
|
55 |
- (NSDictionary *)dictionaryRepresentation; |
|
56 |
/** |
|
57 |
* User ID of event |
|
58 |
*/ |
|
59 |
- (NSString *)userID; |
|
60 |
|
|
61 |
@end |
|
62 |
|
|
63 |
@class TFSScribe; |
|
64 |
|
|
65 |
@protocol TFSScribeErrorDelegate <NSObject> |
|
66 |
|
|
67 |
/** |
|
68 |
* Scribe will call this method on an arbitrary queue if it encounters |
|
69 |
* an internal error. |
|
70 |
*/ |
|
71 |
- (void)scribeService:(TFSScribe *)service didEncounterError:(NSError *)error; |
|
72 |
|
|
73 |
@end |
|
74 |
|
|
75 |
typedef void (^TFSScribeRequestBatchedImpressionEventBlock)(id<TFSScribeEventParameters> scribeEventParameters); |
|
76 |
typedef void (^TFSScribeRequestCompletionBlock)(TFSScribeServiceRequestDisposition disposition); |
|
77 |
|
|
78 |
@protocol TFSScribeRequestHandler <NSObject> |
|
79 |
|
|
80 |
/** |
|
81 |
* TFSScribe will call this method once it has prepared all of the outgoing events. |
|
82 |
* This method will be called on a background queue. |
|
83 |
* |
|
84 |
* @param outgoingEvents Prepared outgoing events |
|
85 |
* @param userID User ID to send events for. Must not be nil. |
|
86 |
* @param completionHandler Execute the completion block once the events have been handled with the appropriate disposition. The completion block can be executed on any queue. |
|
87 |
*/ |
|
88 |
- (void)handleScribeOutgoingEvents:(NSString *)outgoingEvents userID:(NSString *)userID completionHandler:(TFSScribeRequestCompletionBlock)completionHandler; |
|
89 |
|
|
90 |
@optional |
|
91 |
|
|
92 |
/** |
|
93 |
* When flushing a user ID, this method will be called with an array of impressions |
|
94 |
* for you to batch. After the method is executed, the impressions will be deleted |
|
95 |
* from the store. If this method is not implemented, impressions will still be |
|
96 |
* deleted. |
|
97 |
* |
|
98 |
* @param impressions Array of TFSScribeImpressions objects. |
|
99 |
* @param batchedHandler Call the batchedHandler block for each event data you generate from the impressions. |
|
100 |
*/ |
|
101 |
- (void)handleImpressionsBatch:(NSArray *)impressions batchedImpressionHandler:(TFSScribeRequestBatchedImpressionEventBlock)batchedHandler; |
|
102 |
|
|
103 |
@end |
|
104 |
|
|
105 |
@interface TFSScribe : NSObject |
|
106 |
|
|
107 |
@property (nonatomic, weak) id<TFSScribeErrorDelegate> errorDelegate; |
|
108 |
|
|
109 |
+ (BOOL)isDebugEnabled; |
|
110 |
+ (void)setDebugEnabled:(BOOL)enabled; |
|
111 |
|
|
112 |
/** |
|
113 |
* Init the scribe. |
|
114 |
* |
|
115 |
* @param storeURL File URL to store the persisted data. Pass nil to use an in-memory store. |
|
116 |
*/ |
|
117 |
- (instancetype)initWithStoreURL:(NSURL *)storeURL; |
|
118 |
|
|
119 |
/** |
|
120 |
* Init the scribe. |
|
121 |
* |
|
122 |
* @param storeURL File URL to store the persisted data. Pass nil to use an in-memory store. |
|
123 |
* @param modelURL File URL to the location of the mom on disk. Not necessary if you are using the framework, or if your mom is in the mainBundle. |
|
124 |
*/ |
|
125 |
- (instancetype)initWithStoreURL:(NSURL *)storeURL modelURL:(NSURL *)modelURL NS_DESIGNATED_INITIALIZER; |
|
126 |
|
|
127 |
/** |
|
128 |
* Opens the scribe asynchronously. |
|
129 |
*/ |
|
130 |
- (void)open; |
|
131 |
|
|
132 |
/** |
|
133 |
* Opens the scribe asynchronously. |
|
134 |
* |
|
135 |
* @param startBlock Block will be executed on a background queue immediately prior to opening. Can be nil. |
|
136 |
* @param completionBlock Block will be executed on a background queue immediately after opening. Can be nil. |
|
137 |
*/ |
|
138 |
- (void)openWithStartBlock:(dispatch_block_t)startBlock completionBlock:(dispatch_block_t)completionBlock; |
|
139 |
|
|
140 |
/** |
|
141 |
* Closes the scribe asynchronously. |
|
142 |
*/ |
|
143 |
- (void)close; |
|
144 |
|
|
145 |
/** |
|
146 |
* Closes the scribe asynchronously. |
|
147 |
* |
|
148 |
* @param startBlock Block will be executed on a background queue immediately prior to closing. Can be nil. |
|
149 |
* @param completionBlock Block will be executed on a background queue immediately after closing. Can be nil. |
|
150 |
*/ |
|
151 |
- (void)closeWithStartBlock:(dispatch_block_t)startBlock completionBlock:(dispatch_block_t)completionBlock; |
|
152 |
|
|
153 |
/** |
|
154 |
* Suspend the scribe's internal background queue. This could be used to improve scrolling performance on older devices. |
|
155 |
*/ |
|
156 |
- (void)suspend; |
|
157 |
/** |
|
158 |
* Resume the scribe's internal background queue. |
|
159 |
*/ |
|
160 |
- (void)resume; |
|
161 |
|
|
162 |
/** |
|
163 |
* Flush all events for the given userID. If no events or impressions exist |
|
164 |
* |
|
165 |
* @param userID User ID to flush events for. Must not be nil. |
|
166 |
* @param requestHandler Once the events have been prepared, the request handler will be called to handle the events. Must not be nil. |
|
167 |
*/ |
|
168 |
- (void)flushUserID:(NSString *)userID requestHandler:(id<TFSScribeRequestHandler>)requestHandler; |
|
169 |
|
|
170 |
/** |
|
171 |
* Flush all events for the given userID. |
|
172 |
* |
|
173 |
* @param userID User ID to flush events for. Must not be nil. |
|
174 |
* @param token A token to be included with notification userInfo dictionaries. |
|
175 |
* @param requestHandler Once the events have been prepared, the request handler will be called to handle the events. Must not be nil. |
|
176 |
*/ |
|
177 |
- (void)flushUserID:(NSString *)userID token:(NSString *)token requestHandler:(id<TFSScribeRequestHandler>)requestHandler; |
|
178 |
|
|
179 |
/** |
|
180 |
* Delete all events for the given userID. Events for user ID of 0 (anonymous user) will not be deleted. |
|
181 |
*/ |
|
182 |
- (void)deleteUserID:(NSString *)userID; |
|
183 |
|
|
184 |
/** |
|
185 |
* Schedule an event to be added to scribe. |
|
186 |
*/ |
|
187 |
- (void)enqueueEvent:(id<TFSScribeEventParameters>)eventParameters; |
|
188 |
|
|
189 |
/** |
|
190 |
* Schedule an impression to be added to scribe. |
|
191 |
*/ |
|
192 |
- (void)enqueueImpression:(NSData *)contentData eventName:(NSString *)eventName query:(NSString *)query clientVersion:(NSString *)clientVersion userID:(NSString *)userID; |
|
193 |
|
|
194 |
#if UIAUTOMATION |
|
195 |
- (void)clearScribeDatabase; |
|
196 |
#endif |
|
197 |
|
|
198 |
@end |