Skip to content

Commit f1dc617

Browse files
gast04aalekz
andauthored
Improvements to request interceptor and event queue init, remove unused CoreTelephony references (#146)
* make use of non main thread dependend token generation * Update comments * remove unecessary parse * allow nil properties * properly set self.task * remove unecessary protocol setting * remove unused var * Don't include JSON payload in flush logging. * Remove comment, change eventQueue property from atomic to nonatomic. --------- Co-authored-by: Alexander Simson <[email protected]>
1 parent f302cfa commit f1dc617

File tree

5 files changed

+26
-30
lines changed

5 files changed

+26
-30
lines changed

Castle/Internal/CASAPIClient.m

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ - (NSURLSessionDataTask *)dataTaskWithPath:(NSString *)path
6666

6767
// Parse response data
6868
if(data) {
69-
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
7069
NSString *contentType = httpResponse.allHeaderFields[@"Content-Type"];
7170

7271
// Check for application/json content-type and parse response

Castle/Internal/CASCustom.m

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,17 @@ + (nullable instancetype)eventWithName:(nullable NSString *)name properties:(nul
3737
CASLog(@"Event name can't be nil.");
3838
return nil;
3939
}
40-
40+
4141
if([name isEqualToString:@""]) {
4242
CASLog(@"Event names must be at least one (1) character long.");
4343
return nil;
4444
}
45+
46+
if(!properties) {
47+
// prevent dropping from nil properties in event, as it is allowed
48+
// per signature
49+
properties = @{};
50+
}
4551

4652
BOOL valid = [CASEvent propertiesContainValidData:properties];
4753
if(!valid) {

Castle/Internal/CASEventQueue.m

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,18 @@ - (instancetype)init
4141
{
4242
self = [super init];
4343
if (self) {
44-
dispatch_sync(CASEventStorageQueue(), ^{
45-
// Read stored queue from disk
46-
self.eventQueue = [self storedQueue].mutableCopy;
47-
48-
// Initialize API client
49-
self.client = [CASAPIClient clientWithConfiguration:[Castle configuration]];
44+
// Immediate initialize
45+
self.eventQueue = [[NSMutableArray alloc] init];
46+
self.client = [CASAPIClient clientWithConfiguration:[Castle configuration]];
47+
48+
dispatch_async(CASEventStorageQueue(), ^{
49+
NSArray *storedEvents = [self storedQueue];
50+
if (storedEvents.count > 0) {
51+
// Prepend to current queue
52+
NSMutableArray *combined = [storedEvents mutableCopy];
53+
[combined addObjectsFromArray:self.eventQueue];
54+
self.eventQueue = combined;
55+
}
5056
});
5157
}
5258
return self;
@@ -233,7 +239,7 @@ - (void)flush
233239

234240
self.task = nil;
235241

236-
CASLog(@"Successfully flushed (%ld) events: %@", monitorModel.events.count, [monitorModel JSONPayload]);
242+
CASLog(@"Successfully flushed (%ld) events", monitorModel.events.count);
237243

238244
if ([self eventQueueExceedsFlushLimit] && self.eventQueue.count > 0) {
239245
CASLog(@"Current event queue still exceeds flush limit. Flush again");

Castle/Internal/CASRequestInterceptor.m

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -43,25 +43,17 @@ + (BOOL)requestIsCacheEquivalent:(NSURLRequest *)a toRequest:(NSURLRequest *)b
4343

4444
- (void)startLoading
4545
{
46-
dispatch_sync(dispatch_get_main_queue(), ^{
47-
// Always flush the queue when a request is intercepted
48-
[Castle flush];
49-
});
50-
5146
NSMutableURLRequest *newRequest = [self.request mutableCopy];
5247
[NSURLProtocol setProperty:@YES forKey:CASRecursiveRequestFlagProperty inRequest:newRequest];
5348

54-
dispatch_sync(dispatch_get_main_queue(), ^{
55-
// Set custom header
56-
[newRequest setValue:[Castle createRequestToken] forHTTPHeaderField:CastleRequestTokenHeaderName];
57-
});
49+
// Set custom header
50+
[newRequest setValue:[Castle createRequestToken] forHTTPHeaderField:CastleRequestTokenHeaderName];
5851

5952
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
60-
config.protocolClasses = [config.protocolClasses arrayByAddingObject:self.class];
6153
NSURLSession *session = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:nil];
6254

63-
NSURLSessionDataTask *task = [session dataTaskWithRequest:newRequest];
64-
[task resume];
55+
self.task = [session dataTaskWithRequest:newRequest];
56+
[self.task resume];
6557
}
6658

6759
- (void)stopLoading
@@ -76,10 +68,8 @@ - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task willPer
7668
if (response) {
7769
NSMutableURLRequest *redirectRequest = [newRequest mutableCopy];
7870

79-
dispatch_sync(dispatch_get_main_queue(), ^{
80-
// Set custom header
81-
[redirectRequest setValue:[Castle createRequestToken] forHTTPHeaderField:CastleRequestTokenHeaderName];
82-
});
71+
// Set custom header
72+
[redirectRequest setValue:[Castle createRequestToken] forHTTPHeaderField:CastleRequestTokenHeaderName];
8373

8474
[[self client] URLProtocol:self wasRedirectedToRequest:redirectRequest redirectResponse:response];
8575

Castle/Public/Castle.m

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@
99

1010
#import <UIKit/UIKit.h>
1111

12-
#import <CoreTelephony/CTCarrier.h>
13-
#import <CoreTelephony/CTTelephonyNetworkInfo.h>
14-
1512
#import "CASAPIClient.h"
1613
#import "CASUtils.h"
1714
#import "CASEvent.h"
@@ -81,8 +78,6 @@ - (NSURL *)baseURL
8178

8279
NSString *const CastleRequestTokenHeaderName = @"X-Castle-Request-Token";
8380

84-
static CTTelephonyNetworkInfo *_telephonyNetworkInfo;
85-
8681
@interface Castle ()
8782
@property (nonatomic, strong, nullable) CastleConfiguration *configuration;
8883
@property (nonatomic, strong, nullable) CASEventQueue *eventQueue;

0 commit comments

Comments
 (0)