Skip to content

Commit 94492ca

Browse files
Added configurable NSURLSessionConfiguration, and retry attempts.
1 parent 48ca4e5 commit 94492ca

File tree

5 files changed

+80
-8
lines changed

5 files changed

+80
-8
lines changed

Parse/Internal/Commands/CommandRunner/URLSession/PFURLSessionCommandRunner.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,17 @@ NS_ASSUME_NONNULL_BEGIN
1616
@interface PFURLSessionCommandRunner : NSObject <PFCommandRunning>
1717

1818
- (instancetype)init NS_UNAVAILABLE;
19+
- (instancetype)initWithDataSource:(id<PFInstallationIdentifierStoreProvider>)dataSource
20+
sessionConfiguration:(NSURLSessionConfiguration *)configuration
21+
retryAttempts:(NSUInteger)retryAttempts
22+
applicationId:(NSString *)applicationId
23+
clientKey:(NSString *)clientKey;
24+
25+
+ (instancetype)commandRunnerWithDataSource:(id<PFInstallationIdentifierStoreProvider>)dataSource
26+
sessionConfiguration:(NSURLSessionConfiguration *)configuration
27+
retryAttempts:(NSUInteger)retryAttempts
28+
applicationId:(NSString *)applicationId
29+
clientKey:(NSString *)clientKey;
1930

2031
@end
2132

Parse/Internal/Commands/CommandRunner/URLSession/PFURLSessionCommandRunner.m

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
@interface PFURLSessionCommandRunner () <PFURLSessionDelegate>
3434

3535
@property (nonatomic, strong) NSNotificationCenter *notificationCenter;
36+
@property (nonatomic, assign) NSUInteger retryAttempts;
3637

3738
@end
3839

@@ -53,8 +54,23 @@ - (instancetype)init {
5354
- (instancetype)initWithDataSource:(id<PFInstallationIdentifierStoreProvider>)dataSource
5455
applicationId:(NSString *)applicationId
5556
clientKey:(NSString *)clientKey {
56-
NSURLSessionConfiguration *configuration = [[self class] _urlSessionConfigurationForApplicationId:applicationId
57-
clientKey:clientKey];
57+
return [self initWithDataSource:dataSource
58+
sessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]
59+
retryAttempts:PFCommandRunningDefaultMaxAttemptsCount
60+
applicationId:applicationId
61+
clientKey:clientKey];
62+
63+
}
64+
65+
- (instancetype)initWithDataSource:(id<PFInstallationIdentifierStoreProvider>)dataSource
66+
sessionConfiguration:(NSURLSessionConfiguration *)configuration
67+
retryAttempts:(NSUInteger)retryAttempts
68+
applicationId:(NSString *)applicationId
69+
clientKey:(NSString *)clientKey {
70+
configuration = [[self class] _modifyUrlSessionConfiguration:configuration
71+
forApplicationId:applicationId
72+
clientKey:clientKey];
73+
5874
PFURLSession *session = [PFURLSession sessionWithConfiguration:configuration delegate:self];
5975
PFCommandURLRequestConstructor *constructor = [PFCommandURLRequestConstructor constructorWithDataSource:dataSource];
6076
self = [self initWithDataSource:dataSource
@@ -63,6 +79,7 @@ - (instancetype)initWithDataSource:(id<PFInstallationIdentifierStoreProvider>)da
6379
notificationCenter:[NSNotificationCenter defaultCenter]];
6480
if (!self) return nil;
6581

82+
_retryAttempts = retryAttempts;
6683
_applicationId = [applicationId copy];
6784
_clientKey = [clientKey copy];
6885

@@ -77,6 +94,7 @@ - (instancetype)initWithDataSource:(id<PFInstallationIdentifierStoreProvider>)da
7794
if (!self) return nil;
7895

7996
_initialRetryDelay = PFCommandRunningDefaultRetryDelay;
97+
_retryAttempts = PFCommandRunningDefaultMaxAttemptsCount;
8098

8199
_requestConstructor = requestConstructor;
82100
_session = session;
@@ -91,6 +109,18 @@ + (instancetype)commandRunnerWithDataSource:(id<PFInstallationIdentifierStorePro
91109
return [[self alloc] initWithDataSource:dataSource applicationId:applicationId clientKey:clientKey];
92110
}
93111

112+
+ (instancetype)commandRunnerWithDataSource:(id<PFInstallationIdentifierStoreProvider>)dataSource
113+
sessionConfiguration:(NSURLSessionConfiguration *)configuration
114+
retryAttempts:(NSUInteger)retryAttempts
115+
applicationId:(NSString *)applicationId
116+
clientKey:(NSString *)clientKey {
117+
return [[self alloc] initWithDataSource:dataSource
118+
sessionConfiguration:configuration
119+
retryAttempts:retryAttempts
120+
applicationId:applicationId
121+
clientKey:clientKey];
122+
}
123+
94124
///--------------------------------------
95125
#pragma mark - Dealloc
96126
///--------------------------------------
@@ -192,7 +222,7 @@ - (BFTask *)_performCommandRunningBlock:(nonnull id (^)())block
192222
return [self _performCommandRunningBlock:block
193223
withCancellationToken:cancellationToken
194224
delay:delay
195-
forAttempts:PFCommandRunningDefaultMaxAttemptsCount];
225+
forAttempts:_retryAttempts];
196226
}
197227

198228
- (BFTask *)_performCommandRunningBlock:(nonnull id (^)())block
@@ -209,7 +239,7 @@ - (BFTask *)_performCommandRunningBlock:(nonnull id (^)())block
209239
if ([[task.error userInfo][@"temporary"] boolValue] && attempts > 1) {
210240
PFLogError(PFLoggingTagCommon,
211241
@"Network connection failed. Making attempt %lu after sleeping for %f seconds.",
212-
(unsigned long)(PFCommandRunningDefaultMaxAttemptsCount - attempts + 1), (double)delay);
242+
(unsigned long)(_retryAttempts - attempts + 1), (double)delay);
213243

214244
return [[BFTask taskWithDelay:(int)(delay * 1000)] continueWithBlock:^id(BFTask *task) {
215245
return [self _performCommandRunningBlock:block
@@ -226,9 +256,10 @@ - (BFTask *)_performCommandRunningBlock:(nonnull id (^)())block
226256
#pragma mark - NSURLSessionConfiguration
227257
///--------------------------------------
228258

229-
+ (NSURLSessionConfiguration *)_urlSessionConfigurationForApplicationId:(NSString *)applicationId
230-
clientKey:(NSString *)clientKey {
231-
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
259+
+ (NSURLSessionConfiguration *)_modifyUrlSessionConfiguration:(NSURLSessionConfiguration *)configuration
260+
forApplicationId:(NSString *)applicationId
261+
clientKey:(NSString *)clientKey {
262+
configuration = [configuration copy];
232263

233264
// No cookies, they are bad for you.
234265
configuration.HTTPCookieAcceptPolicy = NSHTTPCookieAcceptPolicyNever;
@@ -243,7 +274,11 @@ + (NSURLSessionConfiguration *)_urlSessionConfigurationForApplicationId:(NSStrin
243274
NSDictionary *headers = [PFCommandURLRequestConstructor defaultURLRequestHeadersForApplicationId:applicationId
244275
clientKey:clientKey
245276
bundle:bundle];
246-
configuration.HTTPAdditionalHeaders = headers;
277+
278+
NSMutableDictionary *existingHeaders = [configuration.HTTPAdditionalHeaders mutableCopy];
279+
[existingHeaders addEntriesFromDictionary:headers];
280+
281+
configuration.HTTPAdditionalHeaders = existingHeaders;
247282

248283
return configuration;
249284
}

Parse/Internal/ParseManager.m

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,8 @@ - (PFInstallationIdentifierStore *)installationIdentifierStore {
313313
dispatch_sync(_commandRunnerAccessQueue, ^{
314314
if (!_commandRunner) {
315315
_commandRunner = [PFURLSessionCommandRunner commandRunnerWithDataSource:self
316+
sessionConfiguration:self.configuration.URLSessionConfiguration
317+
retryAttempts:self.configuration.URLSessionRetryAttempts
316318
applicationId:self.configuration.applicationId
317319
clientKey:self.configuration.clientKey];
318320
}

Parse/ParseClientConfiguration.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ NS_ASSUME_NONNULL_BEGIN
2323
@property (nullable, nonatomic, copy, readwrite) NSString *applicationGroupIdentifier;
2424
@property (nullable, nonatomic, copy, readwrite) NSString *containingApplicationBundleIdentifier;
2525

26+
@property (null_resettable, nonatomic, copy, readwrite) NSURLSessionConfiguration *URLSessionConfiguration;
27+
@property (nonatomic, assign, readwrite) NSUInteger URLSessionRetryAttempts;
28+
2629
@end
2730

2831
/*!
@@ -38,6 +41,9 @@ NS_ASSUME_NONNULL_BEGIN
3841
@property (nullable, nonatomic, copy, readonly) NSString *applicationGroupIdentifier;
3942
@property (nullable, nonatomic, copy, readonly) NSString *containingApplicationBundleIdentifier;
4043

44+
@property (null_resettable, nonatomic, copy, readonly) NSURLSessionConfiguration *URLSessionConfiguration;
45+
@property (nonatomic, assign, readonly) NSUInteger URLSessionRetryAttempts;
46+
4147
+ (instancetype)configurationWithBlock:(void(^)(id<ParseMutableClientConfiguration> configuration))configurationBlock;
4248

4349
+ (instancetype)new NS_UNAVAILABLE;

Parse/ParseClientConfiguration.m

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#import "ParseClientConfiguration.h"
1111

1212
#import "PFHash.h"
13+
#import "PFCommandRunningConstants.h"
1314

1415
@interface ParseClientConfiguration()
1516

@@ -21,6 +22,9 @@ @interface ParseClientConfiguration()
2122
@property (nullable, nonatomic, copy, readwrite) NSString *applicationGroupIdentifier;
2223
@property (nullable, nonatomic, copy, readwrite) NSString *containingApplicationBundleIdentifier;
2324

25+
@property (null_resettable, nonatomic, copy, readwrite) NSURLSessionConfiguration *URLSessionConfiguration;
26+
@property (nonatomic, assign, readwrite) NSUInteger URLSessionRetryAttempts;
27+
2428
@end
2529

2630
// We must implement the protocol here otherwise clang issues warnings about non-matching property declarations.
@@ -38,6 +42,9 @@ - (instancetype)initWithConfigurationBlock:(void (^)(id<ParseMutableClientConfig
3842
self = [super init];
3943
if (!self) return nil;
4044

45+
_URLSessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
46+
_URLSessionRetryAttempts = PFCommandRunningDefaultMaxAttemptsCount;
47+
4148
configurationBlock(self);
4249

4350
return self;
@@ -47,6 +54,17 @@ + (instancetype)configurationWithBlock:(void (^)(id<ParseMutableClientConfigurat
4754
return [[self alloc] initWithConfigurationBlock:configurationBlock];
4855
}
4956

57+
///--------------------------------------
58+
#pragma mark - Properties
59+
///--------------------------------------
60+
61+
- (void)setURLSessionConfiguration:(NSURLSessionConfiguration *)URLSessionConfiguration {
62+
if (URLSessionConfiguration == nil) {
63+
URLSessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
64+
}
65+
_URLSessionConfiguration = URLSessionConfiguration;
66+
}
67+
5068
///--------------------------------------
5169
#pragma mark - NSObject
5270
///--------------------------------------

0 commit comments

Comments
 (0)