这是我的代码:
case NSStreamEventHasBytesAvailable:
if (theStream == inputStream) {
uint8_t buffer[1024];
long len;
NSMutableString *total = [[NSMutableString alloc] init];
while ([inputStream hasBytesAvailable]) {
len = [inputStream read:buffer maxLength:sizeof(buffer)];
if (len > 0) {
[total appendString: [[NSString alloc] initWithBytes:buffer length:len encoding:NSASCIIStringEncoding]];
currentOperation.response = total;
}
}
[super executeOperation:currentOperation];
}
任何可能导致这种情况的想法?我100%确定邮件是从我的服务器正确发送的。当我在while循环上设置断点时,如果我禁用断点,它会在几行中删除消息,问题就会消失
答案 0 :(得分:0)
我找到了一个解决方案,通过验证JSON字符串并循环它是否有效,直到它有效
case NSStreamEventHasBytesAvailable:
{
if (theStream == inputStream) {
uint8_t buffer[1024];
long len;
NSMutableString *totalJSONString = [[NSMutableString alloc] init];
NSData *data;
id json;
do {
len = [inputStream read:buffer maxLength:sizeof(buffer)];
if (len > 0) {
NSError *error = nil;
[totalJSONString appendString:[[NSString alloc] initWithBytes:buffer length:len encoding:NSUTF8StringEncoding]];
}
data = [totalJSONString dataUsingEncoding:NSUTF8StringEncoding];
json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
} while (json == nil);
currentOperation.response = totalJSONString;
NSLog(@"%@", totalJSONString);
totalJSONString = (NSMutableString *) @"";
[super executeOperation:currentOperation];
}
break;
}