Skip to content

Instantly share code, notes, and snippets.

@jlindsey
Created July 29, 2010 22:10
Show Gist options
  • Save jlindsey/499392 to your computer and use it in GitHub Desktop.
Save jlindsey/499392 to your computer and use it in GitHub Desktop.
Example of GCD
- (void)writeData:(NSData *)data toOutputStream:(NSOutputStream *)stream {
dispatch_async(dispatch_get_current_queue(), ^(void){
BOOL sent = NO;
do {
if (outputStreamReady) {
outputStreamReady = NO;
uint8_t *readBytes = (uint8_t *)[data bytes];
int data_len = [data length];
uint8_t buf[data_len];
(void)memcpy(buf, readBytes, data_len);
[stream write:(const uint8_t *)buf maxLength:data_len];
sent = YES;
}
} while(!sent);
});
}
@jlindsey
Copy link
Author

This function is from a chat program I'm writing.

The outputStreamReady var is defined on the instance level and changed based on the delegate methods from the NSOutputStream object connected to the server. I was having a problem where in some cases the user would type something to be sent out over the chat but the output stream was not ready to send more data so the message would just be lost.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment