ios快速写入流

时间:2013-10-29 06:03:23

标签: ios iphone objective-c

循环下面的代码时遇到问题:

    NSString *response  = [NSString stringWithFormat:@"{\"id\":\"%@\",\"type\":\"%@\",\"msg\":\"%@\"}",userID, method, msg];
    NSData *data = [[NSData alloc] initWithData:[response dataUsingEncoding:NSASCIIStringEncoding]];
    [outputStream write:[data bytes] maxLength:[data length]];

这是我打开流的方式:

// Open connection to server
- (void)initNetworkCommunication {
    isConnected = TRUE;
    CFReadStreamRef readStream;
    CFWriteStreamRef writeStream;
    CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)@"url.com", 8080, &readStream, &writeStream);
    inputStream = (__bridge NSInputStream *)readStream;
    outputStream = (__bridge NSOutputStream *)writeStream;

    [inputStream setDelegate:self];
    [outputStream setDelegate:self];

    [inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    [outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];

    [inputStream open];
    [outputStream open];
}

问题是,当快速写入流时,它将所有消息合并为一个,我的服务器当时只能解析一个JSON

有没有办法实现一次只发送1个response字符串?像队列一样?

人们通常如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

如果您'刷新'应该解决问题的输出流。 Here是一个类似的问题,建议您在字符串中添加“\ n”或“\ r \ n”以将其刷新。