Skip to content

Instantly share code, notes, and snippets.

@Goles
Forked from landonf/cancel_ticket_example.m
Created August 23, 2013 18:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Goles/6322549 to your computer and use it in GitHub Desktop.
Save Goles/6322549 to your computer and use it in GitHub Desktop.
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