Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
S
shopify_iossdk
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
cemarose
shopify_iossdk
Commits
a22d1f29
Commit
a22d1f29
authored
May 16, 2016
by
Dima Bart
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Implement thread-safe state changes in BUYOperation. Refactor BUYClient.
parent
df7ba142
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
106 additions
and
56 deletions
+106
-56
BUYClient.h
Mobile Buy SDK/Mobile Buy SDK/Data/BUYClient.h
+1
-1
BUYClient.m
Mobile Buy SDK/Mobile Buy SDK/Data/BUYClient.m
+10
-15
BUYOperation.h
Mobile Buy SDK/Mobile Buy SDK/Operations/BUYOperation.h
+3
-0
BUYOperation.m
Mobile Buy SDK/Mobile Buy SDK/Operations/BUYOperation.m
+81
-11
BUYRequestOperation.h
...e Buy SDK/Mobile Buy SDK/Operations/BUYRequestOperation.h
+2
-8
BUYRequestOperation.m
...e Buy SDK/Mobile Buy SDK/Operations/BUYRequestOperation.m
+9
-21
No files found.
Mobile Buy SDK/Mobile Buy SDK/Data/BUYClient.h
View file @
a22d1f29
...
@@ -96,7 +96,7 @@ NS_ASSUME_NONNULL_BEGIN
...
@@ -96,7 +96,7 @@ NS_ASSUME_NONNULL_BEGIN
/**
/**
* Queue where on which all request operation are executed
* Queue where on which all request operation are executed
*/
*/
@property
(
nonatomic
,
strong
,
nonnull
)
NSOperationQueue
*
requestQueue
;
@property
(
nonatomic
,
strong
,
readonly
,
nonnull
)
NSOperationQueue
*
requestQueue
;
/**
/**
* Queue where callbacks will be called
* Queue where callbacks will be called
...
...
Mobile Buy SDK/Mobile Buy SDK/Data/BUYClient.m
View file @
a22d1f29
...
@@ -32,12 +32,7 @@ static NSString * const BUYClientJSONMimeType = @"application/json";
...
@@ -32,12 +32,7 @@ static NSString * const BUYClientJSONMimeType = @"application/json";
@interface
BUYClient
()
<
NSURLSessionDelegate
>
@interface
BUYClient
()
<
NSURLSessionDelegate
>
@property
(
nonatomic
,
strong
)
NSString
*
shopDomain
;
@property
(
nonatomic
,
strong
)
NSString
*
apiKey
;
@property
(
nonatomic
,
strong
)
NSString
*
appId
;
@property
(
nonatomic
,
strong
)
NSURLSession
*
session
;
@property
(
nonatomic
,
strong
)
NSURLSession
*
session
;
@property
(
nonatomic
,
strong
)
NSString
*
merchantId
;
@end
@end
...
@@ -58,15 +53,15 @@ static NSString * const BUYClientJSONMimeType = @"application/json";
...
@@ -58,15 +53,15 @@ static NSString * const BUYClientJSONMimeType = @"application/json";
self
=
[
super
init
];
self
=
[
super
init
];
if
(
self
)
{
if
(
self
)
{
self
.
modelManager
=
[
BUYModelManager
modelManager
];
_
modelManager
=
[
BUYModelManager
modelManager
];
self
.
shopDomain
=
shopDomain
;
_
shopDomain
=
shopDomain
;
self
.
apiKey
=
apiKey
;
_
apiKey
=
apiKey
;
self
.
appId
=
appId
;
_
appId
=
appId
;
self
.
applicationName
=
[[
NSBundle
mainBundle
]
infoDictionary
][
@"CFBundleName"
]
?:
@""
;
_
applicationName
=
[[
NSBundle
mainBundle
]
infoDictionary
][
@"CFBundleName"
]
?:
@""
;
self
.
queue
=
dispatch_get_main_queue
();
_
queue
=
dispatch_get_main_queue
();
self
.
requestQueue
=
[
NSOperationQueue
new
];
_
requestQueue
=
[
NSOperationQueue
new
];
self
.
session
=
[
self
urlSession
];
_
session
=
[
self
urlSession
];
self
.
pageSize
=
25
;
_
pageSize
=
25
;
}
}
return
self
;
return
self
;
}
}
...
@@ -219,7 +214,7 @@ static NSString * const BUYClientJSONMimeType = @"application/json";
...
@@ -219,7 +214,7 @@ static NSString * const BUYClientJSONMimeType = @"application/json";
-
(
void
)
enableApplePayWithMerchantId
:
(
NSString
*
)
merchantId
-
(
void
)
enableApplePayWithMerchantId
:
(
NSString
*
)
merchantId
{
{
self
.
merchantId
=
merchantId
;
_
merchantId
=
merchantId
;
}
}
@end
@end
Mobile Buy SDK/Mobile Buy SDK/Operations/BUYOperation.h
View file @
a22d1f29
...
@@ -10,4 +10,7 @@
...
@@ -10,4 +10,7 @@
@interface
BUYOperation
:
NSOperation
@interface
BUYOperation
:
NSOperation
-
(
void
)
startExecution
;
-
(
void
)
finishExecution
;
@end
@end
Mobile Buy SDK/Mobile Buy SDK/Operations/BUYOperation.m
View file @
a22d1f29
...
@@ -8,10 +8,15 @@
...
@@ -8,10 +8,15 @@
#import "BUYOperation.h"
#import "BUYOperation.h"
typedef
NS_ENUM
(
NSUInteger
,
BUYOperationState
)
{
BUYOperationStateExecuting
=
1
,
BUYOperationStateFinished
=
2
,
};
@interface
BUYOperation
()
@interface
BUYOperation
()
@property
(
atomic
,
assign
)
BOOL
isExecuting
;
@property
(
nonatomic
,
assign
)
BUYOperationState
state
;
@property
(
atomic
,
assign
)
BOOL
isFinished
;
@property
(
nonatomic
,
strong
)
NSLock
*
lock
;
@end
@end
...
@@ -23,25 +28,90 @@
...
@@ -23,25 +28,90 @@
{
{
self
=
[
super
init
];
self
=
[
super
init
];
if
(
self
)
{
if
(
self
)
{
_lock
=
[
NSLock
new
];
}
}
return
self
;
return
self
;
}
}
#pragma mark - Concurrent -
-
(
BOOL
)
isAsynchronous
{
return
YES
;
}
-
(
BOOL
)
isConcurrent
{
return
YES
;
}
#pragma mark - Accessors -
-
(
BOOL
)
isExecuting
{
return
self
.
state
==
BUYOperationStateExecuting
;
}
-
(
BOOL
)
isFinished
{
return
self
.
state
==
BUYOperationStateFinished
;
}
#pragma mark - Setters -
#pragma mark - Setters -
-
(
void
)
setExecuting
:
(
BOOL
)
executing
-
(
void
)
setState
:
(
BUYOperationState
)
state
{
[
self
.
lock
lock
];
NSString
*
oldPath
=
BUYOperationStateKeyPath
(
self
.
state
);
NSString
*
newPath
=
BUYOperationStateKeyPath
(
state
);
/* ----------------------------------
* We avoid changing state if the new
* state is the same or the operation
* has been cancelled.
*/
if
([
oldPath
isEqualToString
:
newPath
]
||
self
.
isCancelled
)
{
[
self
.
lock
unlock
];
return
;
}
[
self
willChangeValueForKey
:
newPath
];
[
self
willChangeValueForKey
:
oldPath
];
_state
=
state
;
NSLog
(
@"Setting state"
);
[
self
didChangeValueForKey
:
oldPath
];
[
self
didChangeValueForKey
:
newPath
];
[
self
.
lock
unlock
];
}
#pragma mark - Start -
-
(
void
)
start
{
{
[
self
willChangeValueForKey
:
@"isExecuting"
];
[
self
startExecution
];
self
.
isExecuting
=
executing
;
[
self
didChangeValueForKey
:
@"isExecuting"
];
}
}
-
(
void
)
setFinished
:
(
BOOL
)
finished
#pragma mark - Execution -
-
(
void
)
startExecution
{
{
[
self
willChangeValueForKey
:
@"isFinished"
];
self
.
state
=
BUYOperationStateExecuting
;
self
.
isFinished
=
finished
;
NSLog
(
@"Started operation"
);
[
self
didChangeValueForKey
:
@"isFinished"
];
}
-
(
void
)
finishExecution
{
self
.
state
=
BUYOperationStateFinished
;
NSLog
(
@"Finished operation"
);
}
#pragma mark - State -
static
inline
NSString
*
BUYOperationStateKeyPath
(
BUYOperationState
state
)
{
switch
(
state
)
{
case
BUYOperationStateFinished
:
return
@"isFinished"
;
case
BUYOperationStateExecuting
:
return
@"isExecuting"
;
}
return
@""
;
}
}
@end
@end
Mobile Buy SDK/Mobile Buy SDK/Operations/BUYRequestOperation.h
View file @
a22d1f29
...
@@ -11,14 +11,6 @@ NS_ASSUME_NONNULL_BEGIN
...
@@ -11,14 +11,6 @@ NS_ASSUME_NONNULL_BEGIN
@protocol
BUYSerializable
;
@protocol
BUYSerializable
;
typedef
NS_ENUM
(
NSUInteger
,
BUYRequestMethod
)
{
BUYRequestMethodGET
,
BUYRequestMethodPOST
,
BUYRequestMethodPUT
,
BUYRequestMethodPATCH
,
BUYRequestMethodDELETE
,
};
typedef
void
(
^
BUYRequestOperationCompletion
)(
NSDictionary
*
_Nullable
json
,
NSURLResponse
*
_Nullable
response
,
NSError
*
_Nullable
error
);
typedef
void
(
^
BUYRequestOperationCompletion
)(
NSDictionary
*
_Nullable
json
,
NSURLResponse
*
_Nullable
response
,
NSError
*
_Nullable
error
);
@interface
BUYRequestOperation
:
BUYOperation
@interface
BUYRequestOperation
:
BUYOperation
...
@@ -26,6 +18,8 @@ typedef void (^BUYRequestOperationCompletion)(NSDictionary * _Nullable json, NSU
...
@@ -26,6 +18,8 @@ typedef void (^BUYRequestOperationCompletion)(NSDictionary * _Nullable json, NSU
@property
(
strong
,
nonatomic
,
readonly
,
nonnull
)
NSURLSession
*
session
;
@property
(
strong
,
nonatomic
,
readonly
,
nonnull
)
NSURLSession
*
session
;
@property
(
strong
,
nonatomic
,
readonly
,
nonnull
)
NSURLRequest
*
originalRequest
;
@property
(
strong
,
nonatomic
,
readonly
,
nonnull
)
NSURLRequest
*
originalRequest
;
+
(
instancetype
)
operationWithSession
:(
NSURLSession
*
)
session
request
:(
NSURLRequest
*
)
request
payload
:(
id
<
BUYSerializable
>
_Nullable
)
payload
completion
:(
BUYRequestOperationCompletion
)
completion
;
-
(
instancetype
)
initWithSession
:(
NSURLSession
*
)
session
request
:(
NSURLRequest
*
)
request
payload
:(
id
<
BUYSerializable
>
_Nullable
)
payload
completion
:(
BUYRequestOperationCompletion
)
completion
;
-
(
instancetype
)
initWithSession
:(
NSURLSession
*
)
session
request
:(
NSURLRequest
*
)
request
payload
:(
id
<
BUYSerializable
>
_Nullable
)
payload
completion
:(
BUYRequestOperationCompletion
)
completion
;
@end
@end
...
...
Mobile Buy SDK/Mobile Buy SDK/Operations/BUYRequestOperation.m
View file @
a22d1f29
...
@@ -44,6 +44,9 @@ NSString * const kShopifyError = @"shopify";
...
@@ -44,6 +44,9 @@ NSString * const kShopifyError = @"shopify";
@implementation
BUYRequestOperation
@implementation
BUYRequestOperation
#pragma mark - Init -
#pragma mark - Init -
+
(
instancetype
)
operationWithSession
:(
NSURLSession
*
)
session
request
:(
NSURLRequest
*
)
request
payload
:(
id
<
BUYSerializable
>
)
payload
completion
:(
BUYRequestOperationCompletion
)
completion
{
return
[[[
self
class
]
alloc
]
initWithSession
:
session
request
:
request
payload
:
payload
completion
:
completion
];
}
-
(
instancetype
)
initWithSession
:(
NSURLSession
*
)
session
request
:(
NSURLRequest
*
)
request
payload
:(
id
<
BUYSerializable
>
)
payload
completion
:(
BUYRequestOperationCompletion
)
completion
-
(
instancetype
)
initWithSession
:(
NSURLSession
*
)
session
request
:(
NSURLRequest
*
)
request
payload
:(
id
<
BUYSerializable
>
)
payload
completion
:(
BUYRequestOperationCompletion
)
completion
{
{
...
@@ -60,34 +63,32 @@ NSString * const kShopifyError = @"shopify";
...
@@ -60,34 +63,32 @@ NSString * const kShopifyError = @"shopify";
-
(
void
)
finishWithJSON
:(
id
)
JSON
response
:(
NSHTTPURLResponse
*
)
response
-
(
void
)
finishWithJSON
:(
id
)
JSON
response
:(
NSHTTPURLResponse
*
)
response
{
{
[
self
setExecuting
:
NO
];
[
self
finishExecution
];
self
.
completion
(
JSON
,
response
,
nil
);
self
.
completion
(
JSON
,
response
,
nil
);
}
}
-
(
void
)
finishWithError
:(
NSError
*
)
error
response
:(
NSHTTPURLResponse
*
)
response
-
(
void
)
finishWithError
:(
NSError
*
)
error
response
:(
NSHTTPURLResponse
*
)
response
{
{
[
self
setExecuting
:
NO
];
[
self
finishExecution
];
self
.
completion
(
nil
,
response
,
error
);
self
.
completion
(
nil
,
response
,
error
);
}
}
-
(
void
)
finishByCancellation
-
(
void
)
finishByCancellation
{
{
[
self
setFinished
:
YES
];
[
self
finishExecution
];
[
self
setExecuting
:
NO
];
}
}
#pragma mark - Start -
#pragma mark - Start -
-
(
void
)
start
-
(
void
)
start
Execution
{
{
[
super
start
];
if
(
self
.
isCancelled
)
{
if
(
self
.
isCancelled
)
{
[
self
finishByCancellation
];
[
self
finishByCancellation
];
return
;
return
;
}
}
[
self
setExecuting
:
YES
];
[
super
startExecution
];
NSURLSessionDataTask
*
task
=
[
self
.
session
dataTaskWithRequest
:
self
.
originalRequest
completionHandler
:
^
(
NSData
*
data
,
NSURLResponse
*
response
,
NSError
*
error
)
{
NSURLSessionDataTask
*
task
=
[
self
.
session
dataTaskWithRequest
:
self
.
originalRequest
completionHandler
:
^
(
NSData
*
data
,
NSURLResponse
*
response
,
NSError
*
error
)
{
if
(
self
.
isCancelled
)
{
if
(
self
.
isCancelled
)
{
...
@@ -114,17 +115,4 @@ NSString * const kShopifyError = @"shopify";
...
@@ -114,17 +115,4 @@ NSString * const kShopifyError = @"shopify";
[
task
resume
];
[
task
resume
];
}
}
#pragma mark - BUYRequestMethod -
//static inline NSString * BUYRequestMethodString(BUYRequestMethod method)
//{
// switch (method) {
// case BUYRequestMethodGET: return @"GET";
// case BUYRequestMethodPOST: return @"POST";
// case BUYRequestMethodPATCH: return @"PATCH";
// case BUYRequestMethodPUT: return @"PUT";
// case BUYRequestMethodDELETE: return @"DELETE";
// }
//}
@end
@end
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment