@@ -26,12 +26,17 @@ @implementation GDTCCTTestStorage {
26
26
NSMutableDictionary <NSNumber *, NSSet <GDTCOREvent *> *> *_batches;
27
27
}
28
28
29
+ - (instancetype )init {
30
+ self = [super init ];
31
+ if (self) {
32
+ _storedEvents = [[NSMutableDictionary alloc ] init ];
33
+ _batches = [[NSMutableDictionary alloc ] init ];
34
+ }
35
+ return self;
36
+ }
37
+
29
38
- (void )storeEvent : (GDTCOREvent *)event
30
39
onComplete : (void (^_Nullable)(BOOL wasWritten, NSError *_Nullable))completion {
31
- static dispatch_once_t onceToken;
32
- dispatch_once (&onceToken, ^{
33
- self->_storedEvents = [[NSMutableDictionary alloc ] init ];
34
- });
35
40
_storedEvents[event.eventID] = event;
36
41
if (completion) {
37
42
completion (YES , nil );
@@ -44,28 +49,29 @@ - (void)removeEvents:(NSSet<NSString *> *)eventIDs {
44
49
45
50
- (void )batchWithEventSelector : (nonnull GDTCORStorageEventSelector *)eventSelector
46
51
batchExpiration : (nonnull NSDate *)expiration
47
- onComplete :
48
- (nonnull void (^)(NSNumber *_Nullable batchID,
49
- NSSet <GDTCOREvent *> *_Nullable events))onComplete {
50
- static NSInteger count = 0 ;
51
- NSNumber *batchID = @(count);
52
- count++;
53
- static dispatch_once_t onceToken;
54
- dispatch_once (&onceToken, ^{
55
- self->_batches = [[NSMutableDictionary alloc ] init ];
56
- });
57
- NSSet <GDTCOREvent *> *batchEvents = [NSSet setWithArray: [_storedEvents allValues ]];
58
- _batches[batchID] = batchEvents;
59
- [_storedEvents removeAllObjects ];
60
- if (onComplete) {
61
- onComplete (batchID, batchEvents);
52
+ onComplete : (nonnull GDTCORStorageBatchBlock)onComplete {
53
+ if (self.batchWithEventSelectorHandler ) {
54
+ self.batchWithEventSelectorHandler (eventSelector, expiration, onComplete);
55
+ } else {
56
+ [self defaultBatchWithEventSelector: eventSelector
57
+ batchExpiration: expiration
58
+ onComplete: onComplete];
62
59
}
63
60
}
64
61
65
62
- (void )removeBatchWithID : (nonnull NSNumber *)batchID
66
63
deleteEvents : (BOOL )deleteEvents
67
64
onComplete : (void (^_Nullable)(void ))onComplete {
68
- [_batches removeObjectForKey: batchID];
65
+ if (deleteEvents) {
66
+ [_batches removeObjectForKey: batchID];
67
+ [self .removeBatchAndDeleteEventsExpectation fulfill ];
68
+ } else {
69
+ for (GDTCOREvent *batchedEvent in _batches[batchID]) {
70
+ _storedEvents[batchedEvent.eventID] = batchedEvent;
71
+ }
72
+ [self .removeBatchWithoutDeletingEventsExpectation fulfill ];
73
+ }
74
+
69
75
if (onComplete) {
70
76
onComplete ();
71
77
}
@@ -95,7 +101,9 @@ - (void)removeLibraryDataForKey:(nonnull NSString *)key
95
101
}
96
102
97
103
- (void )hasEventsForTarget:(GDTCORTarget)target onComplete:(nonnull void (^)(BOOL ))onComplete {
98
- if (onComplete) {
104
+ if (self.hasEventsForTargetHandler ) {
105
+ self.hasEventsForTargetHandler (target, onComplete);
106
+ } else if (onComplete) {
99
107
onComplete (NO );
100
108
}
101
109
}
@@ -105,12 +113,32 @@ - (void)storageSizeWithCallback:(void (^)(uint64_t storageSize))onComplete {
105
113
106
114
- (void )batchIDsForTarget : (GDTCORTarget)target
107
115
onComplete : (nonnull void (^)(NSSet <NSNumber *> *_Nullable))onComplete {
116
+ [self .batchIDsForTargetExpectation fulfill ];
108
117
if (onComplete) {
109
- onComplete (nil );
118
+ onComplete ([ NSSet setWithArray: [ self ->_batches allKeys ]] );
110
119
}
111
120
}
112
121
113
122
- (void )checkForExpirations {
114
123
}
115
124
125
+ #pragma mark - Default Implementations
126
+
127
+ - (void )defaultBatchWithEventSelector : (nonnull GDTCORStorageEventSelector *)eventSelector
128
+ batchExpiration : (nonnull NSDate *)expiration
129
+ onComplete : (nonnull GDTCORStorageBatchBlock)onComplete {
130
+ static NSInteger count = 0 ;
131
+ NSNumber *batchID = @(count);
132
+ count++;
133
+
134
+ NSSet <GDTCOREvent *> *batchEvents = [NSSet setWithArray: [_storedEvents allValues ]];
135
+ _batches[batchID] = batchEvents;
136
+ [_storedEvents removeAllObjects ];
137
+
138
+ [self .batchWithEventSelectorExpectation fulfill ];
139
+ if (onComplete) {
140
+ onComplete (batchID, batchEvents);
141
+ }
142
+ }
143
+
116
144
@end
0 commit comments