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";
@interface BUYClient () <NSURLSessionDelegate>
@property (nonatomic, strong) NSURLSession *session;
@property (nonatomic, strong) NSOperationQueue *networkQueue;
@end
......@@ -58,26 +59,44 @@ static NSString * const BUYClientJSONMimeType = @"application/json";
_shopDomain = shopDomain;
_apiKey = apiKey;
_appId = appId;
_applicationName = [[NSBundle mainBundle] infoDictionary][@"CFBundleName"] ?: @"";
_callbackQueue = [NSOperationQueue mainQueue];
_requestQueue = [NSOperationQueue new];
_networkQueue = [NSOperationQueue new];
_session = [self urlSession];
_pageSize = 25;
}
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 -
- (NSURLSession *)urlSession
{
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
config.HTTPAdditionalHeaders = [self additionalHeaders];
NSString *bundleIdentifier = [[NSBundle mainBundle] bundleIdentifier];
config.HTTPAdditionalHeaders = @{@"User-Agent": [NSString stringWithFormat:@"Mobile Buy SDK iOS/%@/%@", BUYClientVersionString, bundleIdentifier]};
return [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:self.requestQueue];
return [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:self.networkQueue];
}
- (void)setPageSize:(NSUInteger)pageSize
......@@ -89,23 +108,20 @@ static NSString * const BUYClientJSONMimeType = @"application/json";
- (BUYStatus)statusForStatusCode:(NSUInteger)statusCode error:(NSError *)error
{
BUYStatus status = BUYStatusUnknown;
if (statusCode == BUYStatusPreconditionFailed) {
status = BUYStatusPreconditionFailed;
}
else if (statusCode == BUYStatusNotFound) {
status = BUYStatusNotFound;
}
else if (error || statusCode == BUYStatusFailed) {
status = BUYStatusFailed;
switch ((BUYStatus)statusCode) {
case BUYStatusPreconditionFailed: return BUYStatusPreconditionFailed;
case BUYStatusNotFound: return BUYStatusNotFound;
case BUYStatusFailed: return BUYStatusFailed;
case BUYStatusProcessing: return BUYStatusProcessing;
case BUYStatusComplete: return BUYStatusComplete;
default: {
if (error) {
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
......
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