Created
July 29, 2010 22:10
-
-
Save jlindsey/499392 to your computer and use it in GitHub Desktop.
Example of GCD
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
- (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); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 theNSOutputStream
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.