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}
0 commit comments