Last active
December 21, 2015 14:49
-
-
Save landonf/6322417 to your computer and use it in GitHub Desktop.
Stubbed out cancel ticket example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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