Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Safeguard API usage and delegate reset in ProductsRequestHandler. #569

Merged
merged 1 commit into from
Nov 16, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ - (instancetype)initWithProductsRequest:(SKProductsRequest *)request {
self = [super init];
if (!self) return nil;

_taskCompletionSource = [BFTaskCompletionSource taskCompletionSource];

_productsRequest = request;
_productsRequest.delegate = self;

Expand All @@ -63,8 +65,9 @@ - (void)dealloc {
///--------------------------------------

- (BFTask *)findProductsAsync {
_taskCompletionSource = [BFTaskCompletionSource taskCompletionSource];
[_productsRequest start];
if (!_taskCompletionSource.task.completed) {
[_productsRequest start];
}
return _taskCompletionSource.task;
}

Expand All @@ -73,20 +76,22 @@ - (BFTask *)findProductsAsync {
///--------------------------------------

- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response {
_productsRequest.delegate = nil;

PFProductsRequestResult *result = [[PFProductsRequestResult alloc] initWithProductsResponse:response];
[self.taskCompletionSource trySetResult:result];
}

- (void)request:(SKRequest *)request didFailWithError:(NSError *)error {
[self.taskCompletionSource trySetError:error];

// according to documentation, this method does not call requestDidFinish
request.delegate = nil;
_productsRequest.delegate = nil;

[self.taskCompletionSource trySetError:error];
}

- (void)requestDidFinish:(SKRequest *)request {
// the documentation assures that this is the point safe to get rid of the request
request.delegate = nil;
_productsRequest.delegate = nil;
}

@end