Skip to content

Instantly share code, notes, and snippets.

@landonf
Last active December 21, 2015 14:49
Show Gist options
  • Save landonf/6322417 to your computer and use it in GitHub Desktop.
Save landonf/6322417 to your computer and use it in GitHub Desktop.
Stubbed out cancel ticket example
PLCancelTicketSet *ticketSet = [PLCancelTicketSet ticketSet];
id<PLCancelTicket> httpTicket = [_client connectionWithRequest: req
bodyData: reqData
timeout: timeout
dispatchContext: [PLDirectDispatchContext context]
block: ^(PLHTTPResponse *response, id <PLInputStream> bodyInputStream, NSError *error)
{
// ... check for error ..
PLLimitingInputStreamFilter *inputFilter = [PLLimitingInputStreamFilter filterWithInputStream: bodyInputStream
maximumBytes: MAX_RESOURCE_DOWNLOAD_BYTES];
// Asynchronously copy the response body
PLDataOutputStream *dataStream = [PLDataOutputStream stream];
id<PLCancelTicket> copy = [PLPipeStream copyInputStream: inputFilter
toOutputStream: dataStream
withTimeout: remaining
dispatchContext: dispatchContext
block: ^(NSError *error) {
if (error != nil) {
resultBlock(nil, PLExampleClientErrorWithCode(PLExampleClientErrorConnectionLost, error));
return;
}
/* Success -- decode the result */
NSString *stringResult = [[NSString alloc] initWithData: dataStream.data
encoding: [response textEncodingWithDefaultEncoding: NSUTF8StringEncoding]];
resultBlock(stringResult, nil);
}];
/* Add our copy ticket to the cancel ticket set. */
[ticketSet addCancelTicket: copy];
}
[ticketSet addCancelTicket: httpTicket];
return [PLCancelTicket cancelTicketWithBlock: ^{
/* Cancel the set */
[ticketSet cancel];
/* Let the result ticket perform cleanup */
resultBlock(nil, nil);
}];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment