Skip to content

Commit b6ef32a

Browse files
authored
Reload static context data when the app returns from background. (#856)
* Respond to changes regarding advertising ID. * Removed unnecessary context. * Remove extraneous NSNull handling causing tests to fail. * Added weakify/strongify macros. * Removed extraneous NSNull checking. * Put locking around static context access.
1 parent 24abc1f commit b6ef32a

File tree

4 files changed

+57
-5
lines changed

4 files changed

+57
-5
lines changed

Analytics.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
6E265C791FB1178C0030E08E /* IntegrationsManagerTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6E265C781FB1178C0030E08E /* IntegrationsManagerTest.swift */; };
1212
6EEC1C712017EA370089C478 /* EndToEndTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6EEC1C702017EA370089C478 /* EndToEndTests.swift */; };
1313
A31958EF2385AC3A00A47EFA /* SerializationTests.m in Sources */ = {isa = PBXBuildFile; fileRef = A31958EE2385AC3A00A47EFA /* SerializationTests.m */; };
14+
A352176023AD5825005B07F6 /* SEGMacros.h in Headers */ = {isa = PBXBuildFile; fileRef = A352175F23AD5825005B07F6 /* SEGMacros.h */; settings = {ATTRIBUTES = (Private, ); }; };
1415
EA88A5981DED7608009FB66A /* SEGSerializableValue.h in Headers */ = {isa = PBXBuildFile; fileRef = EA88A5971DED7608009FB66A /* SEGSerializableValue.h */; settings = {ATTRIBUTES = (Public, ); }; };
1516
EA8F09741E24C5C600B8B93F /* MiddlewareTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = EA8F09731E24C5C600B8B93F /* MiddlewareTests.swift */; };
1617
EAA542771EB4035400945DA7 /* TrackingTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = EAA542761EB4035400945DA7 /* TrackingTests.swift */; };
@@ -94,6 +95,7 @@
9495
6EEC1C702017EA370089C478 /* EndToEndTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EndToEndTests.swift; sourceTree = "<group>"; };
9596
89033CBF22319674E6CE7E61 /* Pods_AnalyticsTests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_AnalyticsTests.framework; sourceTree = BUILT_PRODUCTS_DIR; };
9697
A31958EE2385AC3A00A47EFA /* SerializationTests.m */ = {isa = PBXFileReference; indentWidth = 4; lastKnownFileType = sourcecode.c.objc; path = SerializationTests.m; sourceTree = "<group>"; tabWidth = 4; };
98+
A352175F23AD5825005B07F6 /* SEGMacros.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SEGMacros.h; sourceTree = "<group>"; };
9799
D3BF8AE673FE0FD91DF5B503 /* Pods-AnalyticsTests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-AnalyticsTests.release.xcconfig"; path = "Pods/Target Support Files/Pods-AnalyticsTests/Pods-AnalyticsTests.release.xcconfig"; sourceTree = "<group>"; };
98100
EA88A5971DED7608009FB66A /* SEGSerializableValue.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SEGSerializableValue.h; sourceTree = "<group>"; };
99101
EA8F09731E24C5C600B8B93F /* MiddlewareTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MiddlewareTests.swift; sourceTree = "<group>"; };
@@ -347,6 +349,7 @@
347349
EADEB89F1DECD12B005322DA /* SEGUserDefaultsStorage.m */,
348350
EADEB8A01DECD12B005322DA /* SEGUtils.h */,
349351
EADEB8A11DECD12B005322DA /* SEGUtils.m */,
352+
A352175F23AD5825005B07F6 /* SEGMacros.h */,
350353
EADEB8A21DECD12B005322DA /* UIViewController+SEGScreen.h */,
351354
EADEB8A31DECD12B005322DA /* UIViewController+SEGScreen.m */,
352355
);
@@ -410,6 +413,7 @@
410413
EADEB8DE1DECD12B005322DA /* SEGAnalyticsConfiguration.h in Headers */,
411414
EADEB8D61DECD12B005322DA /* UIViewController+SEGScreen.h in Headers */,
412415
EADEB8CF1DECD12B005322DA /* SEGStorage.h in Headers */,
416+
A352176023AD5825005B07F6 /* SEGMacros.h in Headers */,
413417
EADEB8D21DECD12B005322DA /* SEGUserDefaultsStorage.h in Headers */,
414418
EADEB8D01DECD12B005322DA /* SEGStoreKitTracker.h in Headers */,
415419
EADEB8D41DECD12B005322DA /* SEGUtils.h in Headers */,
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//
2+
// SEGMacros.h
3+
// Analytics
4+
//
5+
// Created by Brandon Sneed on 12/20/19.
6+
// Copyright © 2019 Segment. All rights reserved.
7+
//
8+
9+
#ifndef SEGMacros_h
10+
#define SEGMacros_h
11+
12+
#define __deprecated__(s) __attribute__((deprecated(s)))
13+
14+
#define weakify(var) __weak typeof(var) __weak_##var = var;
15+
16+
#define strongify(var) \
17+
_Pragma("clang diagnostic push") \
18+
_Pragma("clang diagnostic ignored \"-Wshadow\"") \
19+
__strong typeof(var) var = __weak_##var; \
20+
_Pragma("clang diagnostic pop")
21+
22+
#endif /* SEGMacros_h */

Analytics/Classes/Internal/SEGSegmentIntegration.m

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#import "SEGReachability.h"
88
#import "SEGHTTPClient.h"
99
#import "SEGStorage.h"
10+
#import "SEGMacros.h"
1011

1112
#if TARGET_OS_IOS
1213
#import <CoreTelephony/CTCarrier.h>
@@ -53,7 +54,7 @@ static BOOL GetAdTrackingEnabled()
5354
@interface SEGSegmentIntegration ()
5455

5556
@property (nonatomic, strong) NSMutableArray *queue;
56-
@property (nonatomic, strong) NSDictionary *cachedStaticContext;
57+
@property (nonatomic, strong) NSDictionary *_cachedStaticContext;
5758
@property (nonatomic, strong) NSURLSessionUploadTask *batchRequest;
5859
@property (nonatomic, assign) UIBackgroundTaskIdentifier flushTaskID;
5960
@property (nonatomic, strong) SEGReachability *reachability;
@@ -115,6 +116,12 @@ - (id)initWithAnalytics:(SEGAnalytics *)analytics httpClient:(SEGHTTPClient *)ht
115116

116117
[NSRunLoop.mainRunLoop addTimer:self.flushTimer
117118
forMode:NSDefaultRunLoopMode];
119+
120+
121+
[[NSNotificationCenter defaultCenter] addObserver:self
122+
selector:@selector(updateStaticContext)
123+
name:UIApplicationWillEnterForegroundNotification
124+
object:nil];
118125
}
119126
return self;
120127
}
@@ -205,6 +212,29 @@ - (NSDictionary *)staticContext
205212
return dict;
206213
}
207214

215+
- (void)updateStaticContext
216+
{
217+
self.cachedStaticContext = [self staticContext];
218+
}
219+
220+
- (NSDictionary *)cachedStaticContext {
221+
__block NSDictionary *result = nil;
222+
weakify(self);
223+
dispatch_sync(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), ^{
224+
strongify(self);
225+
result = self._cachedStaticContext;
226+
});
227+
return result;
228+
}
229+
230+
- (void)setCachedStaticContext:(NSDictionary *)cachedStaticContext {
231+
weakify(self);
232+
dispatch_sync(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), ^{
233+
strongify(self);
234+
self._cachedStaticContext = cachedStaticContext;
235+
});
236+
}
237+
208238
- (NSDictionary *)liveContext
209239
{
210240
NSMutableDictionary *context = [[NSMutableDictionary alloc] init];

Analytics/Classes/Internal/SEGUtils.m

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,6 @@ + (id _Nullable)plistFromData:(NSData *_Nonnull)data
3737

3838
+(id)traverseJSON:(id)object andReplaceWithFilters:(NSDictionary<NSString*, NSString*>*)patterns
3939
{
40-
if (object == nil || object == NSNull.null || [object isKindOfClass:NSNull.class]) {
41-
return object;
42-
}
43-
4440
if ([object isKindOfClass:NSDictionary.class]) {
4541
NSDictionary* dict = object;
4642
NSMutableDictionary* newDict = [NSMutableDictionary dictionaryWithCapacity:dict.count];

0 commit comments

Comments
 (0)