Commit 1c96e165 by Dima Bart

Add separate network request queue to avoid deadlock. Minor refactor.

parent a57f4973
...@@ -34,6 +34,7 @@ static NSString * const BUYClientJSONMimeType = @"application/json"; ...@@ -34,6 +34,7 @@ static NSString * const BUYClientJSONMimeType = @"application/json";
@interface BUYClient () <NSURLSessionDelegate> @interface BUYClient () <NSURLSessionDelegate>
@property (nonatomic, strong) NSURLSession *session; @property (nonatomic, strong) NSURLSession *session;
@property (nonatomic, strong) NSOperationQueue *networkQueue;
@end @end
...@@ -58,26 +59,44 @@ static NSString * const BUYClientJSONMimeType = @"application/json"; ...@@ -58,26 +59,44 @@ static NSString * const BUYClientJSONMimeType = @"application/json";
_shopDomain = shopDomain; _shopDomain = shopDomain;
_apiKey = apiKey; _apiKey = apiKey;
_appId = appId; _appId = appId;
_applicationName = [[NSBundle mainBundle] infoDictionary][@"CFBundleName"] ?: @"";
_callbackQueue = [NSOperationQueue mainQueue]; _callbackQueue = [NSOperationQueue mainQueue];
_requestQueue = [NSOperationQueue new]; _requestQueue = [NSOperationQueue new];
_networkQueue = [NSOperationQueue new];
_session = [self urlSession]; _session = [self urlSession];
_pageSize = 25; _pageSize = 25;
} }
return self; return self;
} }
#pragma mark - Headers -
- (NSString *)applicationName
{
return [[NSBundle mainBundle] infoDictionary][@"CFBundleName"] ?: @"";
}
- (NSString *)bundleIdentifier
{
return [[NSBundle mainBundle] bundleIdentifier];
}
- (NSDictionary *)additionalHeaders
{
return @{
@"User-Agent": [NSString stringWithFormat:@"Mobile Buy SDK iOS/%@/%@", BUYClientVersionString, [self bundleIdentifier]]
};
}
#pragma mark - Accessors - #pragma mark - Accessors -
- (NSURLSession *)urlSession - (NSURLSession *)urlSession
{ {
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration]; NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
config.HTTPAdditionalHeaders = [self additionalHeaders];
NSString *bundleIdentifier = [[NSBundle mainBundle] bundleIdentifier]; return [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:self.networkQueue];
config.HTTPAdditionalHeaders = @{@"User-Agent": [NSString stringWithFormat:@"Mobile Buy SDK iOS/%@/%@", BUYClientVersionString, bundleIdentifier]};
return [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:self.requestQueue];
} }
- (void)setPageSize:(NSUInteger)pageSize - (void)setPageSize:(NSUInteger)pageSize
...@@ -89,23 +108,20 @@ static NSString * const BUYClientJSONMimeType = @"application/json"; ...@@ -89,23 +108,20 @@ static NSString * const BUYClientJSONMimeType = @"application/json";
- (BUYStatus)statusForStatusCode:(NSUInteger)statusCode error:(NSError *)error - (BUYStatus)statusForStatusCode:(NSUInteger)statusCode error:(NSError *)error
{ {
BUYStatus status = BUYStatusUnknown; switch ((BUYStatus)statusCode) {
if (statusCode == BUYStatusPreconditionFailed) { case BUYStatusPreconditionFailed: return BUYStatusPreconditionFailed;
status = BUYStatusPreconditionFailed; case BUYStatusNotFound: return BUYStatusNotFound;
} case BUYStatusFailed: return BUYStatusFailed;
else if (statusCode == BUYStatusNotFound) { case BUYStatusProcessing: return BUYStatusProcessing;
status = BUYStatusNotFound; case BUYStatusComplete: return BUYStatusComplete;
} default: {
else if (error || statusCode == BUYStatusFailed) { if (error) {
status = BUYStatusFailed; return BUYStatusFailed;
} else {
return BUYStatusUnknown;
} }
else if (statusCode == BUYStatusProcessing) {
status = BUYStatusProcessing;
} }
else if (statusCode == BUYStatusComplete) {
status = BUYStatusComplete;
} }
return status;
} }
- (NSError *)errorFromJSON:(NSDictionary *)json response:(NSURLResponse *)response - (NSError *)errorFromJSON:(NSDictionary *)json response:(NSURLResponse *)response
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment